Part Number Hot Search : 
15SHC05I SS443KUA 300R1 8XC152JD 01456 BYW95 KPC544 E1482S
Product Description
Full Text Search
 

To Download PIC18F-LF1XK50 Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  ? 2010 microchip technology inc. preliminary ds41350e pic18f/lf1xk50 data sheet 20-pin usb flash microcontrollers with nanowatt xlp technology
ds41350e-page 2 preliminary ? 2010 microchip technology inc. information contained in this publication regarding device applications and the like is provided only for your convenience and may be superseded by updates. it is your responsibility to ensure that your application meets with your specifications. microchip makes no representations or warranties of any kind whether express or implied, written or oral, statutory or otherwise, related to the information, including but not limited to its condition, quality, performance, merchantability or fitness for purpose . microchip disclaims all liability arising from this information and its use. use of microchip devices in life support and/or safety applications is entirely at the buyer?s risk, and the buyer agrees to defend, indemnify and hold harmless microchip from any and all damages, claims, suits, or expenses resulting from such use. no licenses are conveyed, implicitly or otherwise, under any microchip intellectual property rights. trademarks the microchip name and logo, the microchip logo, dspic, k ee l oq , k ee l oq logo, mplab, pic, picmicro, picstart, pic 32 logo, rfpic and uni/o are registered trademarks of microchip technology incorporated in the u.s.a. and other countries. filterlab, hampshire, hi-tech c, linear active thermistor, mxdev, mxlab, seeval and the embedded control solutions company are registered trademarks of microchip technology incorporated in the u.s.a. analog-for-the-digital age, appl ication maestro, codeguard, dspicdem, dspicdem.net, dspicworks, dsspeak, ecan, economonitor, fansense, hi-tide, in-circuit serial programming, icsp, mindi, miwi, mpasm, mplab certified logo, mplib, mplink, mtouch, omniscient code generation, picc, picc-18, picdem, picdem.net, pickit, pictail, real ice, rflab, select mode, total endurance, tsharc, uniwindriver, wiperlock and zena are trademarks of microchip tec hnology incorporated in the u.s.a. and other countries. sqtp is a service mark of microchip technology incorporated in the u.s.a. all other trademarks mentioned herein are property of their respective companies. ? 2010, microchip technology incorporated, printed in the u.s.a., all rights reserved. printed on recycled paper. isbn: 978-1-60932-624-1 note the following details of the code protection feature on microchip devices: ? microchip products meet the specification cont ained in their particular microchip data sheet. ? microchip believes that its family of products is one of the mo st secure families of its kind on the market today, when used i n the intended manner and under normal conditions. ? there are dishonest and possibly illegal methods used to breach the code protection feature. all of these methods, to our knowledge, require using the microchip produc ts in a manner outside the operating specifications contained in microchip?s data sheets. most likely, the person doing so is engaged in theft of intellectual property. ? microchip is willing to work with the customer who is concerned about the integrity of their code. ? neither microchip nor any other semiconductor manufacturer c an guarantee the security of their code. code protection does not mean that we are guaranteeing the product as ?unbreakable.? code protection is constantly evolving. we at microchip are co mmitted to continuously improvi ng the code protection features of our products. attempts to break microchip?s c ode protection feature may be a violation of the digital millennium copyright act. if such acts allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that act. microchip received iso/ts-16949:2002 certification for its worldwide headquarters, design and wafer fabrication facilities in chandler and tempe, arizona; gresham, oregon and design centers in california and india. the company?s quality system processes and procedures are for its pic ? mcus and dspic ? dscs, k ee l oq ? code hopping devices, serial eeproms, microperipherals, nonvolatile memory and analog products. in addition, microchip?s quality system for the design and manufacture of development systems is iso 9001:2000 certified.
? 2010 microchip technology inc. preliminary ds41350e-page 3 pic18f/lf1xk50 universal serial bus features: ? usb v2.0 compliant sie ? full speed (12 mb/s) and low speed (1.5 mb/s) ? supports control, interrupt, isochronous and bulk transfers ? supports up to 16 endpoints (8 bidirectional) ? 256-byte dual access ram for usb ? input-change interrupt on d+/d- for detecting physical connection to usb host high performance risc cpu: ? c compiler optimized architecture: - optional extended instruction set designed to optimize re-entrant code - 256 bytes, data eeprom - up to 16 kbytes linear program memory addressing - up to 768 bytes linear data memory addressing ? priority levels for interrupts ? 8 x 8 single-cycle hardware multiplier flexible oscillator structure: ? cpu divider to run the core slower than the usb peripheral ? 16 mhz internal oscillator block: - software selectable frequencies, 31 khz to 16 mhz - provides a complete range of clock speeds from 31 khz to 32 mhz when used with pll - user tunable to compensate for frequency drift ? four crystal modes, up to 48 mhz ? external clock modes, up to 48 mhz ? 4x phase lock loop (pll) ? secondary oscillator using timer1 at 32 khz ? fail-safe clock monitor: - allows for safe shutdown if primary or second- ary oscillator stops ? two-speed oscillator start-up special microcontroller features: ? full 5.5v operation ? pic18f1xk50 ? 1.8v-3.6v operation ? pic18lf1xk50 ? self-programmable under software control ? programmable brown-out reset (bor) - with software enable option ? extended watchdog timer (wdt) - programmable period from 4ms to 131s ? single-supply 3v in-circuit serial programming? (icsp?) via two pins extreme low-power management pic18lf1xk50 with nanowatt xlp: ? sleep mode: 24 na ? watchdog timer: 450 na ? timer1 oscillator: 790 na @ 32 khz analog features: ? analog-to-digital converter (adc) module: - 10-bit resolution, 9 external channels - auto acquisition capability - conversion available during sleep - internal 1.024v fixed voltage reference (fvr) channel - independent input multiplexing ? dual analog comparators - rail-to-rail operation - independent input multiplexing ? voltage reference module: - programmable (% of v dd ), 16 steps - two 16-level voltage ranges using v ref pins - programmable fixed voltage reference (fvr), 3 levels ? on-chip 3.2v ldo regulator ? (pic18f1xk50) peripheral highlights: ? 14 i/o pins plus 1 input-only pin: - high-current sink/source 25 ma/25 ma - 7 programmable weak pull-ups - 7 programmable interrupt-on-change pins - 3 programmable external interrupts - programmable slew rate ? enhanced capture/compare/pwm (eccp) module: - one, two, three, or four pwm outputs - selectable polarity - programmable dead time - auto-shutdown and auto-restart ? master synchronous serial port (mssp) module: - 3-wire spi (supports all 4 modes) -i 2 c? master and slave modes (slave mode address masking) ? enhanced universal synchronous asynchronous receiver transmitter (eusart) module: - supports rs-485, rs-232 and lin 2.0 - rs-232 operation using internal oscillator - auto-baud detect - auto-wake-up on break ? sr latch mode 20-pin usb flash microcontrollers with nanowatt xlp technology
pic18f/lf1xk50 ds41350e-page 4 preliminary ? 2010 microchip technology inc. - pin diagrams pin diagrams device program memory data memory i/o (1) 10-bit a/d (ch) (2) eccp (pwm) mssp eusart comp. timers 8/16-bit usb flash (bytes) # single-word instructions sram (bytes) eeprom (bytes) spi master i 2 c? pic18f13k50/ pic18lf13k50 8k 4096 512 (3) 256 15 11 1 y y 1 2 1/3 y pic18f14k50/ pic18lf14k50 16k 8192 768 (3) 256 15 11 1 y y 1 2 1/3 y note 1: one pin is input only. 2: channel count includes internal fixed voltage refe rence (fvr) and programmable voltage reference (cv ref ) channels. 3: includes the dual port ram used by the usb m odule which is shared with the data memory. 20-pin pdip, ssop, soic (300 mil) 10 2 3 4 5 6 1 8 7 9 11 12 13 14 15 16 19 20 18 17 v dd ra5/ioca5/osc1/clkin ra4/an3/ioca3/osc2/clkout ra3/ioca3/mclr /v pp rc5/ccp1/p1a/t0cki rc4/p1b/c12out/srq rc3/an7/p1c/c12in3-/pgm rc6/an8/ss /t13cki/t1osci rc7/an9/sdo/t1osco rb7/iocb7/tx/ck v ss ra0/ioca0/d+/pgd ra1/ioca1/d-/pgc vusb rc0/an4/c12in+/int0/v ref + rc1/an5/c12in1-/int1/v ref - rc2/an6/p1d/c12in2-/cv ref /int2 rb4 / an10/iocb4/sdi/sda rb5 / an11/iocb5/rx/dt rb6/iocb6/sck/scl pic18f/lf1xk50 20-pin qfn (5x5) 89 2 3 1 14 15 16 10 11 6 12 13 17 18 19 20 7 5 4 pic18f1xk50/ pic18lf1xk50 ra3/mclr /v pp rc5/ccp1/p1a/t0cki rc4/p1b/c12out/srq rc3/an7/p1c/c12in3-/pgm rc6/an8/ss /t13cki/t1osci rc7/an9/sdo/t1osco rb7/tx/ck rb4 / an10/sdi/sda rb5 / an11/rx/dt rb6/sck/scl rc2/an6/p1d/c12in2-/cv ref /int2 rc1/an1/c12in1-/int1/v ref - rc0/an4/c12in+/int0/v ref + vusb ra1/d-/pgc ra0/d+/pgd vss v dd ra4/an3/osc2/clko ra5/osc1/clki
? 2010 microchip technology inc. preliminary ds41350e-page 5 pic18f/lf1xk50 table 1: pic18f/lf1xk50 pin summary pin i/o analog comparator reference eccp eusart mssp timers interrupts pull-up usb basic 19 ra0 ioca0 d+ pgd 18 ra1 ioca1 d- pgc 4 ra3 (1) ioca3 y mclr /v pp 3ra4 an3 ioca4 y osc2/clkout 2 ra5 ioca5 y osc1/clkin 13 rb4 an10 sdi/sda iocb4 y 12 rb5 an11 rx/dt iocb5 y 11 rb6 scl/sck iocb6 y 10 rb7 tx/ck iocb7 y 16 rc0 an4 c12in+ v ref +int0 15 rc1 an5 c12in1- v ref - int1 14 rc2 an6 c12in2- cv ref p1d int2 7 rc3 an7 c12in3- p1c pgm 6 rc4 c12out p1b srq 5 rc5 ccp1/p1a t0cki 8 rc6 an8 ss t13cki/t1osci 9 rc7 an9 sdo t1osco 17 vusb 1 v dd 20 v ss note 1: input only.
pic18f/lf1xk50 ds41350e-page 6 preliminary ? 2010 microchip technology inc. table of contents 1.0 device overview ............................................................................................................. ............................................................. 9 2.0 oscillator module (with fail-safe clock monitor)............................................................................ ........................................... 15 3.0 memory organization ......................................................................................................... ........................................................ 29 4.0 flash program memory........................................................................................................ ...................................................... 51 5.0 data eeprom memory .......................................................................................................... ................................................... 61 6.0 8 x 8 hardware multiplier................................................................................................... ......................................................... 65 7.0 interrupts .................................................................................................................. .................................................................. 67 8.0 low dropout (ldo) voltage regulator ......................................................................................... ............................................. 81 9.0 i/o ports ................................................................................................................... .................................................................. 83 10.0 timer0 module .............................................................................................................. ........................................................... 101 11.0 timer1 module .............................................................................................................. ........................................................... 105 12.0 timer2 module .............................................................................................................. ........................................................... 111 13.0 timer3 module .............................................................................................................. ........................................................... 113 14.0 enhanced capture/compare/pwm (eccp) module................................................................................. ............................... 117 15.0 master synchronous serial port (mssp) module ............................................................................... ..................................... 139 16.0 enhanced universal synchronous asynchronous receiver transmitter (eusart) .................................................. ............. 181 17.0 analog-to-digital converter (adc) module ................................................................................... ........................................... 209 18.0 comparator module.......................................................................................................... ........................................................ 223 19.0 power-managed modes ........................................................................................................ ................................................... 235 20.0 sr latch................................................................................................................... ................................................................ 241 21.0 voltage references ......................................................................................................... ......................................................... 245 22.0 universal serial bus (usb) ................................................................................................. ..................................................... 251 23.0 reset ...................................................................................................................... .................................................................. 277 24.0 special features of the cpu ................................................................................................ .................................................... 291 25.0 instruction set summary .................................................................................................... ...................................................... 309 26.0 development support........................................................................................................ ....................................................... 359 27.0 electrical specifications.................................................................................................. .......................................................... 363 28.0 dc and ac characteristics graphs and tables ................................................................................ ....................................... 397 29.0 packaging information...................................................................................................... ........................................................ 399 appendix a: revision history................................................................................................... .......................................................... 405 appendix b: device differences................................................................................................. ........................................................ 406 index .......................................................................................................................... ........................................................................ 407 the microchip web site ......................................................................................................... ............................................................ 417 customer change notification service ........................................................................................... ................................................... 417 customer support ............................................................................................................... ............................................................... 417 reader response ................................................................................................................ .............................................................. 418 product identification system.................................................................................................. ........................................................... 419
? 2010 microchip technology inc. preliminary ds41350e-page 7 pic18f/lf1xk50 to our valued customers it is our intention to provide our valued customers with the best documentation possible to ensure successful use of your micro chip products. to this end, we will continue to improve our publications to better suit your needs. our publications will be refined and enhanced as new volumes and updates are introduced. if you have any questions or comments regar ding this publication, please contact the marketing communications department via e-mail at docerrors@mail.microchip.com or fax the reader response form in the back of this data sheet to (480) 792-4150. we welcome your feedback. most current data sheet to obtain the most up-to-date version of this data s heet, please register at our worldwide web site at: http://www.microchip.com you can determine the version of a data sheet by examining its literature number found on the bottom outside corner of any page . the last character of the literature number is the vers ion number, (e.g., ds30000a is version a of document ds30000). errata an errata sheet, describing minor operational differences fr om the data sheet and recommended workarounds, may exist for curren t devices. as device/documentation issues become known to us, we will publish an errata sheet. the errata will specify the revisi on of silicon and revision of document to which it applies. to determine if an errata sheet exists for a particular device, please check with one of the following: ? microchip?s worldwide web site; http://www.microchip.com ? your local microchip sales office (see last page) ? the microchip corporate literatu re center; u.s. fax: (480) 792-7277 when contacting a sales office or the literature center, please specify which device, revision of silicon and data sheet (inclu de literature number) you are using. customer notification system register on our web site at www.microchip.com/cn to receive the most current information on all of our products.
pic18f/lf1xk50 ds41350e-page 8 preliminary ? 2010 microchip technology inc. notes:
? 2010 microchip technology inc. preliminary ds41350e-page 9 pic18f1xk50/pic18lf1xk50 1.0 device overview this document contains device specific information for the following devices: this family offers the advantages of all pic18 microcontrollers ? namely, high computational performance at an economical price ? with the addition of high-endurance, flash program memory. on top of these features, the pic18f/lf1xk50 family introduces design enhancements that make these microcontrollers a logical choice for many high- performance, power sensitive applications. 1.1 new core features 1.1.1 nanowatt xlp technology all of the devices in the pic18f/lf1xk50 family incor- porate a range of features that can significantly reduce power consumption during operation. key items include: ? alternate run modes: by clocking the controller from the timer1 source or the internal oscillator block, power consumption during code execution can be reduced by as much as 90%. ? multiple idle modes: the controller can also run with its cpu core disabled but the peripherals still active. in these states, power consumption can be reduced even further, to as little as 4% of normal operation requirements. ? on-the-fly mode switching: the power- managed modes are invoked by user code during operation, allowing the user to incorporate power- saving ideas into their application?s software design. ? low consumption in key modules: the power requirements for both timer1 and the watchdog timer are minimized. see section 27.0 ?electrical specifications? for values. 1.1.2 multiple oscillator options and features all of the devices in the pic18f/lf1xk50 family offer ten different oscillator options, allowing users a wide range of choices in developing application hardware. these include: ? four crystal modes, using crystals or ceramic resonators ? external clock modes, offering the option of using two pins (oscillator input and a divide-by-4 clock output) or one pin (oscillator input, with the sec- ond pin reassigned as general i/o) ? external rc oscillator modes with the same pin options as the external clock modes ? an internal oscillator block which contains a 16 mhz hfintosc oscillator and a 31 khz lfintosc oscillator which together provide 8 user selectable clock frequencies, from 31 khz to 16 mhz. this option frees the two oscillator pins for use as additional general purpose i/o. ? a phase lock loop (pll) frequency multiplier, available to both the high-speed crystal and inter- nal oscillator modes, which allows clock speeds of up to 48 mhz. used with the internal oscillator, the pll gives users a complete selection of clock speeds, from 31 khz to 32 mhz ? all without using an external crystal or clock circuit. besides its availability as a clock source, the internal oscillator block provides a stable reference source that gives the family additional features for robust operation: ? fail-safe clock monitor: this option constantly monitors the main clock source against a refer- ence signal provided by the lfintosc. if a clock failure occurs, the controller is switched to the internal oscillator block, allowing for continued operation or a safe application shutdown. ? two-speed start-up: this option allows the internal oscillator to serve as the clock source from power-on reset, or wake-up from sleep mode, until the primary clock source is available. ? pic18f13k50 ? pic18f14k50 ? pic18lf13k50 ? pic18lf14k50
pic18f1xk50/pic18lf1xk50 ds41350e-page 10 preliminary ? 2010 microchip technology inc. 1.2 other special features ? memory endurance: the flash cells for both program memory and data eeprom are rated to last for many thousands of erase/write cycles ? up to 1k for program memory and 100k for eeprom. data retention without refresh is conservatively estimated to be greater than 40 years. ? self-programmability: these devices can write to their own program memory spaces under internal software control. using a bootloader routine located in the code protected boot block, it is possible to create an application that can update itself in the field. ? extended instruction set: the pic18f/ lf1xk50 family introduces an optional extension to the pic18 instruction set, which adds 8 new instructions and an indexed addressing mode. this extension has been specifically designed to optimize re-entrant application code originally developed in high-level languages, such as c. ? enhanced ccp module: in pwm mode, this module provides 1, 2 or 4 modulated outputs for controlling half-bridge and full-bridge drivers. other features include: - auto-shutdown, for disabling pwm outputs on interrupt or other select conditions - auto-restart, to reactivate outputs once the condition has cleared - output steering to selectively enable one or more of 4 outputs to provide the pwm signal. ? enhanced addressable usart: this serial communication module is capable of standard rs-232 operation and provides support for the lin bus protocol. other enhancements include automatic baud rate detection and a 16-bit baud rate generator for improved resolution. ? 10-bit a/d converter: this module incorporates programmable acquisition time, allowing for a channel to be selected and a conversion to be initiated without waiting for a sampling period and thus, reduce code overhead. ? extended watchdog timer (wdt): this enhanced version incorporates a 16-bit postscaler, allowing an extended time-out range that is stable across operating voltage and temperature. see section 27.0 ?electrical specifications? for time-out periods. 1.3 details on individual family members devices in the pic18f/lf1xk50 family are available in 20-pin packages. block diagrams for the two groups are shown in figure 1-1 . the devices are differentiated from each other in the following ways: 1. flash program memory: ? 8 kbytes for pic18f13k50/pic18lf13k50 ? 16 kbytes for pic18f14k50/pic18lf14k50 2. on-chip 3.2v ldo regulator for pic18f13k50 and pic18f14k50. all other features for devices in this family are identical. these are summarized in ta b l e 1 - 1 . the pinouts for all devices are listed in tab l e 1 and i/o description are in table 1-2 .
? 2010 microchip technology inc. preliminary ds41350e-page 11 pic18f1xk50/pic18lf1xk50 table 1-1: device features for the pi c18f/lf1xk50 (20-pin devices) features pic18f13k50 pic18lf13k50 pic18f14k50 pic18lf14k50 ldo regulator yes no yes no program memory (bytes) 8k 16k program memory (instructions) 4096 8192 data memory (bytes) 512 768 operating frequency dc ? 48 mhz interrupt sources 30 i/o ports ports a, b, c timers 4 enhanced capture/ compare/pwm modules 1 serial communications mssp, enhanced usart, usb 10-bit analog-to-digital module 9 input channels resets (and delays) por, bor, reset instruction, stack full, stack underflow, mclr , wdt (pwrt, ost) instruction set 75 instructions, 83 with extended instruction set enabled packages 20-pin pdip, ssop, soic (300 mil) and qfn (5x5)
pic18f1xk50/pic18lf1xk50 ds41350e-page 12 preliminary ? 2010 microchip technology inc. figure 1-1: pic18f/lf1xk50 block diagram instruction decode and control porta portb portc ra1 ra0 data latch data memory address latch data address<12> 12 access bsr fsr0 fsr1 fsr2 inc/dec logic address 4 12 4 pch pcl pclath 8 31-level stack program counter prodl prodh 8 x 8 multiply 8 bitop 8 8 alu<8> 20 8 8 table pointer<21> inc/dec logic 21 8 data bus<8> table latch 8 ir 12 3 rom latch pclatu pcu note 1: ra3 is only available when mclr functionality is disabled. 2: osc1/clkin and osc2/clkout are only available in select oscillator modes and when these pins are not being used as digital i/o. refer to section 2.0 ?oscillator module? for additional information. 3: pic18f13k50/pic18f14k50 only. eusart comparator mssp 10-bit adc timer2 timer1 timer3 timer0 eccp1 bor data eeprom w instruction bus <16> stkptr bank 8 state machine control signals decode 8 8 power-up timer oscillator start-up timer power-on reset watchdog timer osc1 (2) osc2 (2) v dd , internal oscillator fail-safe clock monitor precision reference band gap v ss mclr (1) block lfintosc oscillator 16 mhz oscillator single-supply programming t1oso t1osi fvr fvr fvr cv ref address latch program memory data latch cv ref ra3 ra4 ra5 rb4 rb5 rb6 rb7 rc0 rc1 rc2 rc3 rc4 rc5 rc6 rc7 (512/768 bytes) v usb usb module usb ldo (3) regulator
? 2010 microchip technology inc. preliminary ds41350e-page 13 pic18f1xk50/pic18lf1xk50 table 1-2: pic18f/lf1xk50 pinout i/o descriptions pin name pin number pin type buffer type description ra0/d+/pgd ra0 d+ pgd 19 i i/o i/o ttl xcvr st digital input usb differential plus line (input/output) icsp? programming data pin ra1/d-/pgc ra1 d- pgc 18 i i/o i/o ttl xcvr st digital input usb differential minus line (input/output) icsp? programming clock pin ra3/mclr /v pp ra3 mclr v pp 4 i i p st st ? master clear (input) or programming voltage (input) digital input active-low master clear with internal pull-up high voltage programming input ra4/an3/osc2/clkout ra4 an3 osc2 clkout 3 i/o i o o ttl analog xtal cmos digital i/o adc channel 3 oscillator crystal output. connect to crystal or resonator in crystal oscillator mode in rc mode, osc2 pin outputs clkout which has 1/4 the frequency of osc1 and denotes the instruction cycle rate ra5/osc1/clkin ra5 osc1 clkin 2 i/o i i ttl xtal cmos digital i/o oscillator crystal input or external clock input st buffer when configured in rc mode; analog other wise external clock source input. always associated with the pin function osc1 (see related osc1/clkin, osc2, clkout pins rb4/an10/sdi/sda rb4 an10 sdi sda 13 i/o i i i/o ttl analog st st digital i/o adc channel 10 spi data in i 2 c? data i/o rb5/an11/rx/dt rb5 an11 rx dt 12 i/o i i i/o tll analog st st digital i/o adc channel 11 eusart asynchronous receive eusart synchronous data (see related rx/tx) rb6/sck/sci rb6 sck sci 11 i/o i/o i/o tll st st digital i/o synchronous serial clock input/output for spi mode synchronous serial clock input/output for i 2 c? mode rb7/tx/ck rb7 tx ck 10 i/o o i/o tll cmos st digital i/o eusart asynchronous transmit eusart synchronous clock (see related rx/dt) legend: ttl = ttl compatible input cmos = cmos compatible input or output st = schmitt trigger input i = input o = output p = power xtal= crystal oscillator xcvr = usb differential transceiver
pic18f1xk50/pic18lf1xk50 ds41350e-page 14 preliminary ? 2010 microchip technology inc. rc0/an4/c12in+/int0/v ref + rc0 an4 c12in+ int0 v ref + 16 i/o i i i i st analog analog st analog digital i/o adc channel 4 comparator c1 and c2 non-inverting input external interrupt 0 comparator reference voltage (high) input rc1/an5/c12in-/int1/v ref - rc1 an5 c12in- int1 v ref - 15 i/o i i i i st analog analog st analog digital i/o adc channel 5 comparator c1 and c2 non-inverting input external interrupt 0 comparator reference voltage (low) input rc2/an6/p1d/c12in2-/cv ref /int2 rc2 an6 p1d c12in2- cv ref int2 14 i/o i o i o i st analog cmos analog analog st digital i/o adc channel 6 enhanced ccp1 pwm output comparator c1 and c2 inverting input comparator reference voltage output external interrupt 0 rc3/an7/p1c/c12in3-/pgm rc3 an7 p1c c12in3- pgm 7 i/o i o i i/o st analog cmos analog st digital i/o adc channel 7 enhanced ccp1 pwm output comparator c1 and c2 inverting input low-voltage icsp programming enable pin rc4/p1b/c12out/srq rc4 p1b c12out srq 6 i/o o o o st cmos cmos cmos digital i/o enhanced ccp1 pwm output comparator c1 and c2 output sr latch output rc5/ccp1/p1a/t0cki rc5 ccp1 p1a t0cki 5 i/o i/o o i st st cmos st digital i/o capture 1 input/compare 1 output/pwm 1 output enhanced ccp1 pwm output timer0 external clock input rc6/an8/ss /t13cki/t1osci rc6 an8 ss t13cki t1osci 8 i/o i i i i st analog ttl st xtal digital i/o adc channel 8 spi slave select input timer0 and timer3 external clock input timer1 oscillator input rc7/an9/sdo/t1osco rc7 an9 sdo t1osco 9 i/o i o o st analog cmos xtal digital i/o adc channel 9 spi data out timer1 oscillator output vss 20 p ? ground reference for logic and i/o pins vdd 1 p ? positive supply for logic and i/o pins v usb 17 p ? positive supply for usb transceiver table 1-2: pic18f/lf1xk50 pinout i/o descriptions (continued) pin name pin number pin type buffer type description legend: ttl = ttl compatible input cmos = cmos compatible input or output st = schmitt trigger input i = input o = output p = power xtal= crystal oscillator xcvr = usb differential transceiver
? 2010 microchip technology inc. preliminary ds41350e-page 15 pic18f/lf1xk50 2.0 oscillator module 2.1 overview the oscillator module has a variety of clock sources and features that allow it to be used in a wide range of applications, maximizing performance and minimizing power consumption. figure 2-1 illustrates a block diagram of the oscillator module. key features of the oscillator module include: ? system clock selection - primary external oscillator - secondary external oscillator - internal oscillator ? oscillator start-up timer ? system clock selection ? clock switching ? 4x phase lock loop frequency multiplier ? cpu clock divider ? usb operation - low speed - full speed ? two-speed start-up mode ? fail-safe clock monitoring 2.2 system clock selection the scs bits of the osccon register select between the following clock sources: ? primary external oscillator ? secondary external oscillator ? internal oscillator table 2-1: system clock selection the default state of the scs bits sets the system clock to be the oscillator defined by the fosc bits of the config1h configuration register. the system clock will always be defined by the fosc bits until the scs bits are modified in software. when the internal oscillator is selected as the system clock, the ircf bits of the osccon register and the intsrc bit of the osctune register will select either the lfintosc or the hfintosc. the lfintosc is selected when the ircf<2:0> = 000 and the intsrc bit is clear. all other combinations of the ircf bits and the intsrc bit will select the hfintosc as the system clock. 2.3 primary external oscillator the primary external oscillator?s mode of operation is selected by setting the fosc<3:0> bits of the config1h configuration register. the oscillator can be set to the following modes: ? lp: low-power crystal ? xt: crystal/ceramic resonator ? hs: high-speed crystal resonator ? rc: external rc oscillator ? ec: external clock additionally, the primary external oscillator may be shut-down under firmware control to save power. note: the frequency of the system clock will be referred to as f osc throughout this document. configuration selection scs <1:0> system clock 1x internal oscillator 01 secondary external oscillator 00 (default after reset) oscillator defined by fosc<3:0>
pic18f/lf1xk50 ds41350e-page 16 preliminary ? 2010 microchip technology inc. figure 2-1: pic ? mcu clock source block diagram 4 x pll fosc<3:0> osc2 osc1 sleep cpu peripherals idlen postscaler mux mux 16 mhz 8 mhz 4 mhz 2 mhz 1 mhz 250 khz 500 khz ircf<2:0> 111 110 101 100 011 010 001 000 31 khz 31 khz lfintosc internal oscillator block clock control scs<1:0> hfintosc 16 mhz 0 1 intsrc primary pic18f/lf1xk50 sleep sleep system secondary t1oscen enable oscillator t1osi t1oso pclken pri_sd ? 2 cpu divider 0 1 1 0 usbdiv fosc<3:0> low speed usb high speed usb pllen spllen oscillator watchdog timer oscillator fail-safe clock two-speed start-up clock 00 1x 01
? 2010 microchip technology inc. preliminary ds41350e-page 17 pic18f/lf1xk50 2.3.1 primary external oscillator shut-down the primary external oscillator can be enabled or dis- abled via software. to enable software control of the primary external oscillator, the pclken bit of the config1h configuration register must be set. with the pclken bit set, the primary external oscillator is controlled by the pri_sd bit of the osccon2 register. the primary external oscillator will be enabled when the pri_sd bit is set, and disabled when the pri_sd bit is clear. 2.3.2 lp, xt and hs oscillator modes the lp, xt and hs modes support the use of quartz crystal resonators or ceramic resonators connected to osc1 and osc2 ( figure 2-2 ). the mode selects a low, medium or high gain setting of the internal inverter- amplifier to support various resonator types and speed. lp oscillator mode selects the lowest gain setting of the internal inverter-amplifier. lp mode current consumption is the least of the three modes. this mode is best suited to drive resonators with a low drive level specification, for example, tuning fork type crystals. xt oscillator mode selects the intermediate gain setting of the internal inverter-amplifier. xt mode current consumption is the medium of the three modes. this mode is best suited to drive resonators with a medium drive level specification. hs oscillator mode selects the highest gain setting of the internal inverter-amplifier. hs mode current consumption is the highest of the three modes. this mode is best suited for resonators that require a high drive setting. figure 2-2 and figure 2-3 show typical circuits for quartz crystal and ceramic resonators, respectively. figure 2-2: quartz crystal operation (lp, xt or hs mode) note: the primary external oscillator cannot be shut down when it is selected as the system clock. to shut down the oscillator, the system clock source must be either the secondary oscillator or the internal oscillator. note 1: quartz crystal characteristics vary according to type, package and manufacturer. the user should consult the manufacturer data sheets for specifications and recommended application. 2: always verify oscillator performance over the v dd and temperature range that is expected for the application. 3: for oscillator design assistance, reference the following microchip applications notes: ? an826, ? crystal oscillator basics and crystal selection for rfpic ? and pic ? devices ? (ds00826) ? an849, ? basic pic ? oscillator design ? (ds00849) ? an943, ? practical pic ? oscillator analysis and design ? (ds00943) ? an949, ? making your oscillator work ? (ds00949) note 1: a series resistor (r s ) may be required for quartz crystals with low drive level. 2: the value of r f varies with the oscillator mode selected (typically between 2 m ? to 10 m ?? . c1 c2 quartz r s (1) osc1/clkin r f (2) sleep to internal logic pic ? mcu crystal osc2/clkout
pic18f/lf1xk50 ds41350e-page 18 preliminary ? 2010 microchip technology inc. figure 2-3: ceramic resonator operation (xt or hs mode) 2.3.3 external rc the external resistor-capacitor (rc) mode supports the use of an external rc circuit. this allows the designer maximum flexibility in frequency choice while keeping costs to a minimum when clock accuracy is not required. in rc mode, the rc circuit connects to osc1, allowing osc2 to be configured as an io or as clkout. the clkout function is selected by the fosc bits of the config1h configuration register. when osc2 is configured as clkout, the frequency at the pin is the frequency of the rc oscillator divided by 4. figure 2-4 shows the external rc mode connections. figure 2-4: external rc modes the rc oscillator frequency is a function of the supply voltage, the resistor rext, the capacitor cext and the operating temperature. other factors affecting the oscillator frequency are: ? input threshold voltage variation ? component tolerances ? variation in capacitance due to packaging 2.3.4 external clock the external clock (ec) mode allows an externally generated logic level clock to be used as the system?s clock source. when operating in this mode, the external clock source is connected to the osc1 allowing osc2 to be configured as an i/o or as clkout. the clkout function is selected by the fosc bits of the config1h configuration register. when osc2 is configured as clkout, the frequency at the pin is the frequency of the ec oscillator divided by 4. three different power settings are available for ec mode. the power settings allow for a reduced i dd of the device, if the ec clock is known to be in a specific range. if there is an expected range of frequencies for the ec clock, select the power mode for the highest frequency. ec low power 0 ? 250 khz ec medium power 250 khz ? 4 mhz ec high power 4 ? 48 mhz 2.4 secondary external oscillator the secondary external oscillator is designed to drive an external 32.768 khz crystal. this oscillator is enabled or disabled by the t1oscen bit of the t1con register. see section 11.0 ?timer1 module? for more information. note 1: a series resistor (r s ) may be required for ceramic resonators with low drive level. 2: the value of r f varies with the oscillator mode selected (typically between 2 m ? to 10 m ?? . 3: an additional parallel feedback resistor (r p ) may be required for proper ceramic resonator operation. c1 c2 ceramic r s (1) osc1/clkin r f (2) sleep to internal logic pic ? mcu r p (3) resonator osc2/clkout osc2/clkout (1) c ext r ext pic ? mcu osc1/clkin f osc /4 or internal clock v dd v ss recommended values: 10 k ? ? r ext ? 100 k ? c ext > 20 pf note 1: alternate pin functions are listed in section 1.0 ?device overview? . 2: output depends upon rc or rcio clock mode. i/o (2)
? 2010 microchip technology inc. preliminary ds41350e-page 19 pic18f/lf1xk50 2.5 internal oscillator the internal oscillator module contains two independent oscillators which are: ? lfintosc: low-frequency internal oscillator ? hfintosc: high-frequency internal oscillator when operating with either oscillator, osc1 will be an i/o and osc2 will be either an i/o or clkout. the clkout function is selected by the fosc bits of the config1h configuration register. when osc2 is configured as clkout, the frequency at the pin is the frequency of the internal oscillator divided by 4. 2.5.1 lfintosc the low-frequency internal oscillator (lfintosc) is a 31 khz internal clock source. the lfintosc oscillator is the clock source for: ? power-up timer ? watchdog timer ? fail-safe clock monitor the lfintosc is enabled when any of the following conditions are true: ? power-up timer is enabled (pwrten = 0 ) ? watchdog timer is enabled (wdten = 1 ) ? watchdog timer is enabled by software (wdten = 0 and swdten = 1 ) ? fail-safe clock monitor is enabled (fcmem = 1 ) ?scs1= 1 and ircf<2:0> = 000 and intsrc = 0 ? fosc<3:0> selects the internal oscillator as the primary clock and ircf<2:0> = 000 and intsrc = 0 ? ieso = 1 (two-speed start-up) and ircf<2:0> = 000 and intsrc = 0 2.5.2 hfintosc the high-frequency internal oscillator (hfintosc) is a precision oscillator that is factory-calibrated to operate at 16 mhz. the output of the hfintosc connects to a postscaler and a multiplexer (see figure 2-1 ). one of eight frequencies can be selected using the ircf<2:0> bits of the osccon register. the following frequencies are available from the hfintosc: ?16 mhz ?8 mhz ?4 mhz ?2 mhz ? 1 mhz (default after reset) ? 500 khz ? 250 khz ?31 khz the hfiofs bit of the osccon register indicates whether the hfintosc is stable. the hfintosc is enabled if any of the following conditions are true: ? scs1 = 1 and ircf<2:0> ? 000 ? scs1 = 1 and ircf<2:0> = 000 and intsrc = 1 ? fosc<3:0> selects the internal oscillator as the primary clock and - ircf<2:0> ? 000 or - ircf<2:0> = 000 and intsrc = 1 ? ieso = 1 (two-speed start-up) and - ircf<2:0> ? 000 or - ircf<2:0> = 000 and intsrc = 1 ?fcmem= 1 (fail safe clock monitoring) and - ircf<2:0> ? 000 or - ircf<2:0> = 000 and intsrc = 1 note 1: selecting 31 khz from the hfintosc oscillator requires ircf<2:0> = 000 and the intsrc bit of the osctune register to be set. if the intsrc bit is clear, the system clock will come from the lfintosc. 2: additional adjustments to the frequency of the hfintosc can made via the osctune registers. see register 2-3 for more details
pic18f/lf1xk50 ds41350e-page 20 preliminary ? 2010 microchip technology inc. 2.6 oscillator control the oscillator control (osccon) ( register 2-1 ) and the oscillator control 2 (osccon2) ( register 2-2 ) registers control the system clock and frequency selection options. register 2-1: osccon: os cillator control register r/w-0 r/w-0 r/w-1 r/w-1 r-q r-0 r/w-0 r/w-0 idlen ircf2 ircf1 ircf0 osts (1) hfiofs scs1 scs0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? q = depends on condition -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 idlen: idle enable bit 1 = device enters idle mode on sleep instruction 0 = device enters sleep mode on sleep instruction bit 6-4 ircf<2:0>: internal oscillator frequency select bits 111 = 16 mhz 110 = 8 mhz 101 = 4 mhz 100 = 2 mhz 011 = 1 mhz (3) 010 = 500 khz 001 = 250 khz 000 = 31 khz (2) bit 3 osts: oscillator start-up time-out status bit (1) 1 = device is running from the clock defined by fosc<2:0> of the config1 register 0 = device is running from the internal oscillator (hfintosc or lfintosc) bit 2 hfiofs: hfintosc frequency stable bit 1 = hfintosc frequency is stable 0 = hfintosc frequency is not stable bit 1-0 scs<1:0>: system clock select bits 1x = internal oscillator block 01 = secondary (timer1) oscillator 00 = primary clock (determined by config1h[fosc<3:0>]). note 1: reset state depends on state of the ieso configuration bit. 2: source selected by the intsrc bit of the osctune register, see text. 3: default output frequency of hfintosc on reset.
? 2010 microchip technology inc. preliminary ds41350e-page 21 pic18f/lf1xk50 register 2-2: osccon2: osci llator control register 2 u-0 u-0 u-0 u-0 u-0 r/w-1 r/w-0 r-x ? ? ? ? ? pri_sd hfiofl lfiofs bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? q = depends on condition -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-3 unimplemented: read as ? 0 ? bit 2 pri_sd: primary oscillator drive circuit shutdown bit 1 = oscillator drive circuit on 0 = oscillator drive circuit off (zero power) bit 1 hfiofl: hfintosc frequency locked bit 1 = hfintosc is in lock 0 = hfintosc has not yet locked bit 0 lfiofs: lfintosc frequency stable bit 1 = lfintosc is stable 0 = lfintosc is not stable
pic18f/lf1xk50 ds41350e-page 22 preliminary ? 2010 microchip technology inc. 2.6.1 osctune register the hfintosc is factory calibrated, but can be adjusted in software by writing to the tun<5:0> bits of the osctune register ( register 2-3 ). the default value of the tun<5:0> is ? 000000 ?. the value is a 6-bit two?s complement number. when the osctune register is modified, the hfintosc frequency will begin shifting to the new frequency. code execution continues during this shift, while giving no indication that the shift has occurred. osctune does not affect the lfintosc frequency. the operation of features that depend on the lfintosc clock source frequency, such as the power-up timer (pwrt), watchdog timer (wdt), fail-safe clock mon- itor (fscm) and peripherals, are not affected by the change in frequency. the osctune register also implements the intsrc and spllen bits, which control certain features of the internal oscillator block. the intsrc bit allows users to select which internal oscillator provides the clock source when the 31 khz frequency option is selected. this is covered in greater detail in section 2.5.1 ?lfintosc? . the spllen bit controls the operation of the frequency multiplier. for more details about the function of the spllen bit see section 2.9 ?4x phase lock loop frequency multiplier? register 2-3: osctune: osci llator tuning register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 intsrc spllen tun5 tun4 tun3 tun2 tun1 tun0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 intsrc: internal oscillator low-frequency source select bit 1 = 31.25 khz device clock derived from 16 mhz hfintosc source (divide-by-512 enabled) 0 = 31 khz device clock derived directly from lfintosc internal oscillator bit 6 spllen: software controlled frequency multiplier pll bit 1 = pll enabled (for hfintosc 8 mhz only) 0 = pll disabled bit 5-0 tun<5:0>: frequency tuning bits 011111 = maximum frequency 011110 = ? ? ? 000001 = 000000 = oscillator module is running at the factory calibrated frequency. 111111 = ? ? ? 100000 = minimum frequency
? 2010 microchip technology inc. preliminary ds41350e-page 23 pic18f/lf1xk50 2.7 oscillator start-up timer the primary external oscillator, when configured for lp, xt or hs modes, incorporates an oscillator start-up timer (ost). the ost ensures that the oscillator starts and provides a stable clock to the oscillator module. the ost times out when 1024 oscillations on osc1 have occurred. during the ost period, with the system clock set to the primary external oscillator, the program counter does not increment suspending program execution. the ost period will occur following: ? power-on reset (por) ? brown-out reset (bor) ? wake-up from sleep ? oscillator being enabled ? expiration of power-up timer (pwrt) in order to minimize latency between external oscillator start-up and code execution, the two-speed start-up mode can be selected. see section 2.12 ?two-speed start-up mode? for more information. 2.8 clock switching the device contains circuitry to prevent clock ?glitches? due to a change of the system clock source. to accomplish this, a short pause in the system clock occurs during the clock switch. if the new clock source is not stable (e.g., ost is active), the device will continue to execute from the old clock source until the new clock source becomes stable. the timing of a clock switch is as follows: 1. scs<1:0> bits of the osccon register are modified. 2. the system clock will continue to operate from the old clock until the new clock is ready. 3. clock switch circuitry waits for two consecutive rising edges of the old clock after the new clock is ready. 4. the system clock is held low, starting at the next falling edge of the old clock. 5. clock switch circuitry waits for an additional two rising edges of the new clock. 6. on the next falling edge of the new clock, the low hold on the system clock is release and the new clock is switched in as the system clock. 7. clock switch is complete. refer to figure 2-5 for more details. figure 2-5: clock switch timing old clock new clock ircf <2:0> system clock start-up time (1) clock sync running high speed low speed select old select new new clk ready low speed high speed old clock new clock ircf <2:0> system clock start-up time (1) clock sync running select old select new new clk ready note 1: start-up time includes t ost (1024 t osc ) for external clocks, plus t pll (approx. 2 ms) for hspll mode.
pic18f/lf1xk50 ds41350e-page 24 preliminary ? 2010 microchip technology inc. table 2-2: examples of dela ys due to clock switching 2.9 4x phase lock loop frequency multiplier a phase locked loop (pll) circuit is provided as an option for users who wish to use a lower-frequency external oscillator or to operate at 32 mhz with the hfintosc. the pll is designed for an input frequency from 4 mhz to 12 mhz. the pll multiplies its input frequency by a factor of four when the pll is enabled. this may be useful for customers who are concerned with emi, due to high-frequency crystals. two bits control the pll: the pllen bit of the config1h configuration register and the spllen bit of the osctune register. the pll is enabled when the pllen bit is set and it is under software control when the pllen bit is cleared. table 2-3: pll configuration 2.9.1 32 mhz internal oscillator frequency selection the internal oscillator block can be used with the 4x pll associated with the external oscillator block to produce a 32 mhz internal system clock source. the following settings are required to use the 32 mhz inter- nal clock source: ? the fosc bits in config1h must be set to use the intosc source as the device system clock (fosc<3:0> = 1000 or 1001 ). ? the scs bits in the osccon register must be cleared to use the clock determined by fosc<3:0> in config1h (scs<1:0> = 00 ). ? the ircf bits in the osccon register must be set to the 8 mhz hfintosc set to use (ircf<2:0> = 110 ). ? the spllen bit in the osctune register must be set to enable the 4xpll, or the pllen bit of config1h must be progr mmed to a ? 1 ?. the 4xpll is not available for use with the internal oscillator when the scs bits of the osccon register are set to ? 1x ?. the scs bits must be set to ? 00 ? to use the 4xpll with the internal oscillator. 2.10 cpu clock divider the cpu clock divider allows the system clock to run at a slower speed than the low/full speed usb module clock while sharing the same clock source. only the oscillator defined by the settings of the fosc bits of the config1h configuration register may be used with the cpu clock divider. the cpu clock divider is controlled by the cpudiv bits of the config1l configuration register. setting the cpudiv bits will set the system clock to: ? equal the clock speed of the usb module ? half the clock speed of the usb module ? one third the clock speed of the usb module ? one fourth the clock speed of the usb module for more information on the cpu clock divider, see figure 2-1 and register 24-1 config1l. switch from switch to oscillator delay sleep/por lfintosc hfintosc oscillator warm-up delay (t warm ) sleep/por lp, xt, hs 1024 clock cycles sleep/por ec, rc 8 clock cycles pllen spllen pll status 1x pll enabled 01 pll enabled 00 pll disabled note: when using the pllen bit of config1h, the 4xpll cannot be disabled by software and the 8 mhz hfintosc option will no longer be available.
? 2010 microchip technology inc. preliminary ds41350e-page 25 pic18f/lf1xk50 2.11 usb operation the usb module is designed to operate in two different modes: ? low speed ? full speed because of timing requirements imposed by the usb specifications, the primary external oscillator is required for the usb module. the fosc bits of the config1h configuration register must be set to either external clock (ec) high-power or hs mode with a clock frequency of 6, 12 or 48 mhz. 2.11.1 low speed operation for low speed usb operation, a 6 mhz clock is required for the usb module. to generate the 6 mhz clock, only 2 oscillator modes are allowed: ? ec high-power mode ?hs mode table 2-4 shows the recommended clock mode for low-speed operation. 2.11.2 full-speed operation for full-speed usb operation, a 48 mhz clock is required for the usb module. to generate the 48 mhz clock, only 2 oscillator modes are allowed: ? ec high-power mode ?hs mode table 2-5 shows the recommended clock mode for full- speed operation. note: users must run usb low speed operation using a cpu clock frequency of 24 mhz or slower (64 mhz is optimal). if anything higher than 24 mhz is used, a firmware delay of at least 14 instruction cycles is required.
pic18f/lf1xk50 ds41350e-page 26 preliminary ? 2010 microchip technology inc. table 2-4: low speed usb clock settings table 2-5: full-speed usb clock settings clock mode clock frequency usbdiv 4x pll enabled cpudiv<1:0> system clock frequency (mhz) ec high/hs 12 mhz 1 yes 00 48 01 24 10 16 11 12 no 00 12 01 6 10 4 11 3 6 mhz 0 yes 00 24 01 12 10 8 11 6 no 00 6 01 3 10 2 11 1.5 note: the system clock frequency in ta bl e 2 - 4 only applies if the osccon register bits scs<1:0> = 00 . by changing these bits, the system clock can operate down to 31 khz. clock mode clock frequency 4x pll enabled cpudiv<1:0> system clock frequency (mhz) ec high 48 mhz no 00 48 01 24 10 16 11 12 ec high/hs 12 mhz yes 00 48 01 24 10 16 11 12 note: the system clock frequency in the above table only applies if the osccon register bits scs<1:0> = 00 . by changing these bits, the system clock can operate down to 31 khz.
? 2010 microchip technology inc. preliminary ds41350e-page 27 pic18f/lf1xk50 2.12 two-speed start-up mode two-speed start-up mode provides additional power savings by minimizing the latency between external oscillator start-up timer (ost) and code execution. in applications that make heavy use of the sleep mode, two-speed start-up will remove the ost period, which can reduce the overall power consumption of the device. two-speed start-up mode is enabled by setting the ieso bit of the config1h configuration register. with two-speed start-up enabled, the device will execute instructions using the internal oscillator during the primary external oscillator ost period. when the system clock is set to the primary external oscillator and the oscillator is configured for lp, xt or hs modes, the device will not execute code during the ost period. the ost will suspend program execution until 1024 oscillations are counted. two-speed start-up mode minimizes the delay in code execution by operating from the internal oscillator while the ost is active. the system clock will switch back to the primary external oscillator after the ost period has expired. two-speed start-up will become active after: ? power-on reset (por) ? power-up timer (pwrt), if enabled ? wake-up from sleep the osts bit of the osccon register reports which oscillator the device is currently using for operation. the device is running from the oscillator defined by the fosc bits of the config1h configuration register when the osts bit is set. the device is running from the internal oscillator when the osts bit is clear. 2.13 fail-safe clock monitor the fail-safe clock monitor (fscm) allows the device to continue operating should the external oscillator fail. the fscm can detect oscillator failure any time after the oscillator start-up timer (ost) has expired. the fscm is enabled by setting the fcmen bit in the config1h configuration register. the fscm is applicable to all external oscillator modes (lp, xt, hs, ec and rc). figure 2-6: fscm block diagram 2.13.1 fail-safe detection the fscm module detects a failed oscillator by comparing the external oscillator to the fscm sample clock. the sample clock is generated by dividing the lfintosc by 64. see figure 2-6 . inside the fail detector block is a latch. the external clock sets the latch on each falling edge of the external clock. the sample clock clears the latch on each rising edge of the sample clock. a failure is detected when an entire half- cycle of the sample clock elapses before the primary clock goes low. 2.13.2 fail-safe operation when the external clock fails, the fscm switches the device clock to an internal clock source and sets the bit flag oscfif of the pir2 register. the oscfif flag will generate an interrupt if the oscfie bit of the pie2 register is also set. the device firmware can then take steps to mitigate the problems that may arise from a failed clock. the system clock will continue to be sourced from the internal clock source until the device firmware successfully restarts the external oscillator and switches back to external operation. an automatic transition back to the failed clock source will not occur. the internal clock source chosen by the fscm is determined by the ircf<2:0> bits of the osccon register. this allows the internal oscillator to be configured before a failure occurs. external lfintosc 64 s r q 31 khz (~32 ? s) 488 hz (~2 ms) clock monitor latch clock failure detected oscillator clock q sample clock
pic18f/lf1xk50 ds41350e-page 28 preliminary ? 2010 microchip technology inc. 2.13.3 fail-safe condition clearing the fail-safe condition is cleared by either one of the following: ? any reset ? by toggling the scs1 bit of the osccon register both of these conditions restart the ost. while the ost is running, the device continues to operate from the intosc selected in osccon. when the ost times out, the fail-safe condition is cleared and the device automatically switches over to the external clock source. the fail-safe condition need not be cleared before the oscfif flag is cleared. 2.13.4 reset or wake-up from sleep the fscm is designed to detect an oscillator failure after the oscillator start-up timer (ost) has expired. the ost is used after waking up from sleep and after any type of reset. the ost is not used with the ec or rc clock modes so that the fscm will be active as soon as the reset or wake-up has completed. when the fscm is enabled, the two-speed start-up is also enabled. therefore, the device will always be executing code while the ost is operating. figure 2-7: fscm timing diagram table 2-6: summary of registers asso ciated with clock sources note: due to the wide range of oscillator start-up times, the fail-safe circuit is not active during oscillator start-up (i.e., after exiting reset or sleep). after an appropriate amount of time, the user should check the osts bit of the osccon register to verify the oscillator start-up and that the system clock switchover has successfully completed. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page config1h ieso fcmen pclken pllen fosc3 fosc2 fosc1 fosc0 296 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 285 osccon idlen ircf2 ircf1 ircf0 osts hfiofs scs1 scs0 286 osctune intsrc spllen tun5 tun4 tun3 tun2 tun1 tun0 288 pie2 oscfie c1ie c2ie eeie bclie usbie tmr3ie ?288 pir2 oscfif c1if c2if eeif bclif usbif tmr3if ?288 t1con rd16 t1run t1ckps1 t1ckps0 t1oscen t1sync tmr1cs tmr1on 105 legend: x = unknown, u = unchanged, ? = unimplemented locations read as ? 0 ?. shaded cells are not used by oscillators. note 1: other (non power-up) resets include mclr reset and watchdog timer reset during normal operation. oscfif system clock output sample clock failure detected oscillator failure note: the system clock is normally at a much higher frequency than the sample clock. the relative frequencies in this example have been chosen for clarity. (q) te s t test test clock monitor output
? 2010 microchip technology inc. preliminary ds41350e-page 29 pic18f1xk50/pic18lf1xk50 3.0 memory organization there are three types of memory in pic18 enhanced microcontroller devices: ? program memory ? data ram ? data eeprom as harvard architecture devices, the data and program memories use separate busses; this allows for concur- rent access of the two memory spaces. the data eeprom, for practical purposes, can be regarded as a peripheral device, since it is addressed and accessed through a set of control registers. additional detailed information on the operation of the flash program memory is provided in section 4.0 ?flash program memory? . data eeprom is discussed separately in section 5.0 ?data eeprom memory? . 3.1 program memory organization pic18 microcontrollers implement a 21-bit program counter, which is capable of addressing a 2-mbyte program memory space. accessing a location between the upper boundary of the physically implemented memory and the 2-mbyte address will return all ? 0 ?s (a nop instruction). this family of devices contain the following: ? pic18f13k50: 8 kbytes of flash memory, up to 4,096 single-word instructions ? pic18f14k50: 16 kbytes of flash memory, up to 8,192 single-word instructions pic18 devices have two interrupt vectors and one reset vector. the reset vector address is at 0000h and the interrupt vector addresses are at 0008h and 0018h. the program memory map for pic18f/lf1xk50 devices is shown in figure 3-1 . memory block details are shown in figure 24-2 . figure 3-1: program memory map and stack for pic18f/lf1xk50 devices pc<20:0> stack level 1 ? stack level 31 reset vector low priority interrupt vector ? ? call,rcall,return retfie,retlw 21 0000h 0018h high priority interrupt vector 0008h user memory space 1fffffh 4000h 3fffh 200000h on-chip program memory read ? 0 ? 1fffh 2000h on-chip program memory read ? 0 ? pic18f14k50 pic18f13k50
pic18f1xk50/pic18lf1xk50 ds41350e-page 30 preliminary ? 2010 microchip technology inc. 3.1.1 program counter the program counter (pc) specifies the address of the instruction to fetch for execution. the pc is 21 bits wide and is contained in three separate 8-bit registers. the low byte, known as the pcl register, is both readable and writable. the high byte, or pch register, contains the pc<15:8> bits; it is not directly readable or writable. updates to the pch register are performed through the pclath register. the upper byte is called pcu. this register contains the pc<20:16> bits; it is also not directly readable or writable. updates to the pcu register are performed through the pclatu register. the contents of pclath and pclatu are transferred to the program counter by any operation that writes pcl. similarly, the upper two bytes of the program counter are transferred to pclath and pclatu by an operation that reads pcl. this is useful for computed offsets to the pc (see section 3.1.4.1 ?computed goto? ). the pc addresses bytes in the program memory. to prevent the pc from becoming misaligned with word instructions, the least significant bit (lsb) of pcl is fixed to a value of ? 0 ?. the pc increments by 2 to address sequential instructions in the program mem- ory. the call , rcall , goto and program branch instructions write to the program counter directly. for these instructions, the contents of pclath and pclatu are not transferred to the program counter. 3.1.2 return address stack the return address stack allows any combination of up to 31 program calls and interrupts to occur. the pc is pushed onto the stack when a call or rcall instruction is executed or an interrupt is acknowledged. the pc value is pulled off the stack on a return, retlw or a retfie instruction. pclatu and pclath are not affected by any of the return or call instructions. the stack operates as a 31-word by 21-bit ram and a 5-bit stack pointer, stkptr. the stack space is not part of either program or data space. the stack pointer is readable and writable and the address on the top of the stack is readable and writable through the top-of- stack (tos) special file registers. data can also be pushed to, or popped from the stack, using these registers. a call type instruction causes a push onto the stack; the stack pointer is first incremented and the location pointed to by the stack pointer is written with the contents of the pc (already pointing to the instruction following the call ). a return type instruction causes a pop from the stack; the contents of the location pointed to by the stkptr are transferred to the pc and then the stack pointer is decremented. the stack pointer is initialized to ? 00000 ? after all resets. there is no ram associated with the location corresponding to a stack pointer value of ? 00000 ?; this is only a reset value. status bits indicate if the stack is full or has overflowed or has underflowed. 3.1.2.1 top-of-stack access only the top of the return address stack (tos) is readable and writable. a set of three registers, tosu:tosh:tosl, hold the contents of the stack location pointed to by the stkptr register ( figure 3-2 ). this allows users to implement a software stack if necessary. after a call, rcall or interrupt, the software can read the pushed value by reading the tosu:tosh:tosl registers. these values can be placed on a user defined software stack. at return time, the software can return these values to tosu:tosh:tosl and do a return. the user must disable the global interrupt enable bits while accessing the stack to prevent inadvertent stack corruption. figure 3-2: return address stack and associated registers 00011 001a34h 11111 11110 11101 00010 00001 00000 00010 return address stack <20:0> to p - o f - st a c k 000d58h tosl tosh tosu 34h 1ah 00h stkptr<4:0> top-of-stack registers stack pointer
? 2010 microchip technology inc. preliminary ds41350e-page 31 pic18f1xk50/pic18lf1xk50 3.1.2.2 return stack pointer (stkptr) the stkptr register ( register 3-1 ) contains the stack pointer value, the stkful (stack full) bit and the stkunf (stack underflow) bits. the value of the stack pointer can be 0 through 31. the stack pointer incre- ments before values are pushed onto the stack and decrements after values are popped off the stack. on reset, the stack pointer value will be zero. the user may read and write the stack pointer value. this fea- ture can be used by a real-time operating system (rtos) for return stack maintenance. after the pc is pushed onto the stack 31 times (without popping any values off the stack), the stkful bit is set. the stkful bit is cleared by software or by a por. the action that takes place when the stack becomes full depends on the state of the stvren (stack over- flow reset enable) configuration bit. (refer to section 24.1 ?configuration bits? for a description of the device configuration bits.) if stvren is set (default), the 31st push will push the (pc + 2) value onto the stack, set the stkful bit and reset the device. the stkful bit will remain set and the stack pointer will be set to zero. if stvren is cleared, the stkful bit will be set on the 31st push and the stack pointer will increment to 31. any additional pushes will not overwrite the 31st push and stkptr will remain at 31. when the stack has been popped enough times to unload the stack, the next pop will return a value of zero to the pc and sets the stkunf bit, while the stack pointer remains at zero. the stkunf bit will remain set until cleared by software or until a por occurs. 3.1.2.3 push and pop instructions since the top-of-stack is readable and writable, the ability to push values onto the stack and pull values off the stack without disturbing normal program execution is a desirable feature. the pic18 instruction set includes two instructions, push and pop , that permit the tos to be manipulated under software control. tosu, tosh and tosl can be modified to place data or a return address on the stack. the push instruction places the current pc value onto the stack. this increments the stack pointer and loads the current pc value onto the stack. the pop instruction discards the current tos by decre- menting the stack pointer. the previous value pushed onto the stack then becomes the tos value. note: returning a value of zero to the pc on an underflow has the effect of vectoring the program to the reset vector, where the stack conditions can be verified and appropriate actions can be taken. this is not the same as a reset, as the contents of the sfrs are not affected. register 3-1: stkptr: stack pointer register r/c-0 r/c-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 stkful (1) stkunf (1) ? sp4 sp3 sp2 sp1 sp0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented c = clearable only bit -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 stkful: stack full flag bit (1) 1 = stack became full or overflowed 0 = stack has not become full or overflowed bit 6 stkunf: stack underflow flag bit (1) 1 = stack underflow occurred 0 = stack underflow did not occur bit 5 unimplemented: read as ? 0 ? bit 4-0 sp<4:0>: stack pointer location bits note 1: bit 7 and bit 6 are cleared by user software or by a por.
pic18f1xk50/pic18lf1xk50 ds41350e-page 32 preliminary ? 2010 microchip technology inc. 3.1.2.4 stack full and underflow resets device resets on stack overflow and stack underflow conditions are enabled by setting the stvren bit in configuration register 4l. when stvren is set, a full or underflow will set the appropriate stkful or stkunf bit and then cause a device reset. when stvren is cleared, a full or underflow condition will set the appropriate stkful or stkunf bit but not cause a device reset. the stkful or stkunf bits are cleared by the user software or a power-on reset. 3.1.3 fast register stack a fast register stack is provided for the status, wreg and bsr registers, to provide a ?fast return? option for interrupts. the stack for each register is only one level deep and is neither readable nor writable. it is loaded with the current value of the corresponding register when the processor vectors for an interrupt. all inter- rupt sources will push values into the stack registers. the values in the registers are then loaded back into their associated registers if the retfie, fast instruction is used to return from the interrupt. if both low and high priority interrupts are enabled, the stack registers cannot be used reliably to return from low priority interrupts. if a high priority interrupt occurs while servicing a low priority interrupt, the stack register values stored by the low priority interrupt will be overwritten. in these cases, users must save the key registers by software during a low priority interrupt. if interrupt priority is not used, all interrupts may use the fast register stack for returns from interrupt. if no interrupts are used, the fast register stack can be used to restore the status, wreg and bsr registers at the end of a subroutine call. to use the fast register stack for a subroutine call, a call label , fast instruction must be executed to save the status, wreg and bsr registers to the fast register stack. a return , fast instruction is then executed to restore these registers from the fast register stack. example 3-1 shows a source code example that uses the fast register stack during a subroutine call and return. example 3-1: fast register stack code example 3.1.4 look-up tables in program memory there may be programming situations that require the creation of data structures, or look-up tables, in program memory. for pic18 devices, look-up tables can be implemented in two ways: ? computed goto ? table reads 3.1.4.1 computed goto a computed goto is accomplished by adding an offset to the program counter. an example is shown in example 3-2 . a look-up table can be formed with an addwf pcl instruction and a group of retlw nn instructions. the w register is loaded with an offset into the table before executing a call to that table. the first instruction of the called routine is the addwf pcl instruction. the next instruction executed will be one of the retlw nn instructions that returns the value ? nn ? to the calling function. the offset value (in wreg) specifies the number of bytes that the program counter should advance and should be multiples of 2 (lsb = 0 ). in this method, only one data byte may be stored in each instruction location and room on the return address stack is required. example 3-2: computed goto using an offset value 3.1.4.2 table reads and table writes a better method of storing data in program memory allows two bytes of data to be stored in each instruction location. look-up table data may be stored two bytes per pro- gram word by using table reads and writes. the table pointer (tblptr) register specifies the byte address and the table latch (tablat) register contains the data that is read from or written to program memory. data is transferred to or from program memory one byte at a time. table read and table write operations are discussed further in section 4.1 ?table reads and table writes? . call sub1, fast ;status, wreg, bsr ;saved in fast register ;stack ? ? sub1 ? ? return, fast ;restore values saved ;in fast register stack movf offset, w call table org nn00h table addwf pcl retlw nnh retlw nnh retlw nnh . . .
? 2010 microchip technology inc. preliminary ds41350e-page 33 pic18f1xk50/pic18lf1xk50 3.2 pic18 instruction cycle 3.2.1 clocking scheme the microcontroller clock input, whether from an internal or external source, is internally divided by four to generate four non-overlapping quadrature clocks (q1, q2, q3 and q4). internally, the program counter is incremented on every q1; the instruction is fetched from the program memory and latched into the instruction register during q4. the instruction is decoded and executed during the following q1 through q4. the clocks and instruction execution flow are shown in figure 3-3 . 3.2.2 instruction flow/pipelining an ?instruction cycle? consists of four q cycles: q1 through q4. the instruction fetch and execute are pipelined in such a manner that a fetch takes one instruction cycle, while the decode and execute take another instruction cycle. however, due to the pipelining, each instruction effectively executes in one cycle. if an instruction causes the program counter to change (e.g., goto ), then two cycles are required to complete the instruction ( example 3-3 ). a fetch cycle begins with the program counter (pc) incrementing in q1. in the execution cycle, the fetched instruction is latched into the instruction register (ir) in cycle q1. this instruction is then decoded and executed during the q2, q3 and q4 cycles. data memory is read during q2 (operand read) and written during q4 (destination write). figure 3-3: clock/ instruction cycle example 3-3: instruction pipeline flow q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 osc1 q1 q2 q3 q4 pc osc2/clkout (rc mode) pc pc + 2 pc + 4 fetch inst (pc) execute inst (pc ? 2) fetch inst (pc + 2) execute inst (pc) fetch inst (pc + 4) execute inst (pc + 2) internal phase clock all instructions are single cycle, except for any program branches. these take two cycles since the fetch instruction is ?flushed? from the pipeline while the new instruction is being fetched and then executed. t cy 0t cy 1t cy 2t cy 3t cy 4t cy 5 1. movlw 55h fetch 1 execute 1 2. movwf portb fetch 2 execute 2 3. bra sub_1 fetch 3 execute 3 4. bsf porta, bit3 (forced nop) fetch 4 flush ( nop ) 5. instruction @ address sub_1 fetch sub_1 execute sub_1
pic18f1xk50/pic18lf1xk50 ds41350e-page 34 preliminary ? 2010 microchip technology inc. 3.2.3 instructions in program memory the program memory is addressed in bytes. instructions are stored as either two bytes or four bytes in program memory. the least significant byte (lsb) of an instruction word is always stored in a program memory location with an even address (lsb = 0 ). to maintain alignment with instruction boundaries, the pc increments in steps of 2 and the lsb will always read ? 0 ? (see section 3.1.1 ?program counter? ). figure 3-4 shows an example of how instruction words are stored in the program memory. the call and goto instructions have the absolute program memory address embedded into the instruction. since instructions are always stored on word boundaries, the data contained in the instruction is a word address. the word address is written to pc<20:1>, which accesses the desired byte address in program memory. instruction #2 in figure 3-4 shows how the instruction goto 0006h is encoded in the program memory. program branch instructions, which encode a relative address offset, operate in the same manner. the offset value stored in a branch instruction represents the number of single-word instructions that the pc will be offset by. section 25.0 ?instruction set summary? provides further details of the instruction set. figure 3-4: instructions in program memory 3.2.4 two-word instructions the standard pic18 instruction set has four two-word instructions: call , movff , goto and lsfr . in all cases, the second word of the instruction always has ? 1111 ? as its four most significant bits (msb); the other 12 bits are literal data, usually a data memory address. the use of ? 1111 ? in the 4 msbs of an instruction specifies a special form of nop . if the instruction is executed in proper sequence ? immediately after the first word ? the data in the second word is accessed and used by the instruction sequence. if the first word is skipped for some reason and the second word is executed by itself, a nop is executed instead. this is necessary for cases when the two-word instruction is preceded by a conditional instruction that changes the pc. example 3-4 shows how this works. example 3-4: two- word instructions word address lsb = 1 lsb = 0 ? program memory byte locations ? ? 000000h 000002h 000004h 000006h instruction 1: movlw 055h 0fh 55h 000008h instruction 2: goto 0006h efh 03h 00000ah f0h 00h 00000ch instruction 3: movff 123h, 456h c1h 23h 00000eh f4h 56h 000010h 000012h 000014h note: see section 3.6 ?pic18 instruction execution and the extended instruc- tion set? for information on two-word instructions in the extended instruction set. case 1: object code source code 0110 0110 0000 0000 tstfsz reg1 ; is ram location 0? 1100 0001 0010 0011 movff reg1, reg2 ; no, skip this word 1111 0100 0101 0110 ; execute this word as a nop 0010 0100 0000 0000 addwf reg3 ; continue code case 2: object code source code 0110 0110 0000 0000 tstfsz reg1 ; is ram location 0? 1100 0001 0010 0011 movff reg1, reg2 ; yes, execute this word 1111 0100 0101 0110 ; 2nd word of instruction 0010 0100 0000 0000 addwf reg3 ; continue code
? 2010 microchip technology inc. preliminary ds41350e-page 35 pic18f1xk50/pic18lf1xk50 3.3 data memory organization the data memory in pic18 devices is implemented as static ram. each register in the data memory has a 12-bit address, allowing up to 4096 bytes of data memory. the memory space is divided into as many as 16 banks that contain 256 bytes each. figure 3-5 and figure 3-6 show the data memory organization for the pic18f/lf1xk50 devices. the data memory contains special function registers (sfrs) and general purpose registers (gprs). the sfrs are used for control and status of the controller and peripheral functions, while gprs are used for data storage and scratchpad operations in the user?s application. any read of an unimplemented location will read as ? 0 ?s. the instruction set and architecture allow operations across all banks. the entire data memory may be accessed by direct, indirect or indexed addressing modes. addressing modes are discussed later in this subsection. to ensure that commonly used registers (sfrs and select gprs) can be accessed in a single cycle, pic18 devices implement an access bank. this is a 256-byte memory space that provides fast access to sfrs and the lower portion of gpr bank 0 without using the bank select register (bsr). section 3.3.3 ?access bank? provides a detailed description of the access ram. 3.3.1 usb ram part of the data memory is actually mapped to a special dual access ram. when the usb module is disabled, the gprs in these banks are used like any other gpr in the data memory space. when the usb module is enabled, the memory in these banks is allocated as buffer ram for usb operation. this area is shared between the microcontroller core and the usb serial interface engine (sie) and is used to transfer data directly between the two. it is theoretically possible to use the areas of usb ram that are not allocated as usb buffers for normal scratchpad memory or other variable storage. in practice, the dynamic nature of buffer allocation makes this risky at best. additional information on usb ram and buffer operation is provided in section 22.0 ?universal serial bus (usb)? 3.3.2 bank select register (bsr) large areas of data memory require an efficient addressing scheme to make rapid access to any address possible. ideally, this means that an entire address does not need to be provided for each read or write operation. for pic18 devices, this is accom- plished with a ram banking scheme. this divides the memory space into 16 contiguous banks of 256 bytes. depending on the instruction, each location can be addressed directly by its full 12-bit address, or an 8-bit low-order address and a 4-bit bank pointer. most instructions in the pic18 instruction set make use of the bank pointer, known as the bank select register (bsr). this sfr holds the 4 most significant bits of a location?s address; the instruction itself includes the 8 least significant bits. only the four lower bits of the bsr are implemented (bsr<3:0>). the upper four bits are unused; they will always read ? 0 ? and cannot be written to. the bsr can be loaded directly by using the movlb instruction. the value of the bsr indicates the bank in data memory; the 8 bits in the instruction show the location in the bank and can be thought of as an offset from the bank?s lower boundary. the relationship between the bsrs value and the bank division in data memory is shown in figure 3-5 and figure 3-6 . since up to 16 registers may share the same low-order address, the user must always be careful to ensure that the proper bank is selected before performing a data read or write. for example, writing what should be program data to an 8-bit address of f9h while the bsr is 0fh will end up resetting the program counter. while any bank can be selected, only those banks that are actually implemented can be read or written to. writes to unimplemented banks are ignored, while reads from unimplemented banks will return ? 0 ?s. even so, the status register will still be affected as if the operation was successful. the data memory maps in figure 3-5 and figure 3-6 indicate which banks are implemented. in the core pic18 instruction set, only the movff instruction fully specifies the 12-bit address of the source and target registers. this instruction ignores the bsr completely when it executes. all other instructions include only the low-order address as an operand and must use either the bsr or the access bank to locate their target registers. note: the operation of some aspects of data memory are changed when the pic18 extended instruction set is enabled. see section 3.5 ?data memory and the extended instruction set? for more information.
pic18f1xk50/pic18lf1xk50 ds41350e-page 36 preliminary ? 2010 microchip technology inc. figure 3-5: data memory map for pi c18f13k50/pic18lf13k50 devices bank 0 bank 1 bank 14 bank 15 data memory map bsr<3:0> = 0000 = 0001 = 1111 060h 05fh f60h fffh 00h 5fh 60h ffh access bank when ?a? = 0 : the bsr is ignored and the access bank is used. the first 96 bytes are general purpose ram (from bank 0). the second 160 bytes are special function registers (from bank 15). when ?a? = 1 : the bsr specifies the bank used by the instruction. f5fh f00h effh 1ffh 100h 0ffh 000h access ram ffh 00h ffh 00h ffh 00h gpr gpr sfr access ram high access ram low bank 2 = 0110 = 0010 (sfrs) 2ffh 200h 3ffh 300h 4ffh 400h 5ffh 500h 6ffh 600h 7ffh 700h 8ffh 800h 9ffh 900h affh a00h bffh b00h cffh c00h dffh d00h e00h bank 3 bank 4 bank 5 bank 6 bank 7 bank 8 bank 9 bank 10 bank 11 bank 12 bank 13 ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h = 0011 = 0100 = 0101 = 0111 = 1000 = 1001 = 1010 = 1011 = 1100 = 1101 = 1110 unused read 00h unused unused read 00h f53h sfr (1) note 1: sfrs occupying f53h to f5fh address space are not in the virtual bank (dpram)
? 2010 microchip technology inc. preliminary ds41350e-page 37 pic18f1xk50/pic18lf1xk50 figure 3-6: data memory map for pi c18f14k50/pic18lf14k50 devices bank 0 bank 1 bank 14 bank 15 data memory map bsr<3:0> = 0000 = 0001 = 1111 060h 05fh 00h 5fh 60h ffh access bank when ?a? = 0 : the bsr is ignored and the access bank is used. the first 96 bytes are general purpose ram (from bank 0). the second 160 bytes are special function registers (from bank 15). when ?a? = 1 : the bsr specifies the bank used by the instruction. f00h effh 1ffh 100h 0ffh 000h access ram ffh 00h ffh 00h gpr gpr access ram high access ram low bank 2 = 0110 = 0010 (sfrs) 2ffh 200h 3ffh 300h 4ffh 400h 5ffh 500h 6ffh 600h 7ffh 700h 8ffh 800h 9ffh 900h affh a00h bffh b00h cffh c00h dffh d00h e00h bank 3 bank 4 bank 5 bank 6 bank 7 bank 8 bank 9 bank 10 bank 11 bank 12 bank 13 ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h gpr ffh 00h = 0011 = 0100 = 0101 = 0111 = 1000 = 1001 = 1010 = 1011 = 1100 = 1101 = 1110 unused read 00h note 1: sfrs occupying f53h to f5fh address space are not in the virtual bank f60h fffh f5fh ffh 00h sfr unused f53h sfr (1) (dpram)
pic18f1xk50/pic18lf1xk50 ds41350e-page 38 preliminary ? 2010 microchip technology inc. figure 3-7: use of the bank select register (direct addressing) note 1: the access ram bit of the instruction can be used to force an override of the selected bank (bsr<3:0>) to the registers of the access bank. 2: the movff instruction embeds the entire 12-bit address in the instruction. data memory bank select (2) 7 0 from opcode (2) 0000 000h 100h 200h 300h f00h e00h fffh bank 0 bank 1 bank 2 bank 14 bank 15 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh bank 3 through bank 13 0011 11111111 7 0 bsr (1)
? 2010 microchip technology inc. preliminary ds41350e-page 39 pic18f1xk50/pic18lf1xk50 3.3.3 access bank while the use of the bsr with an embedded 8-bit address allows users to address the entire range of data memory, it also means that the user must always ensure that the correct bank is selected. otherwise, data may be read from or written to the wrong location. this can be disastrous if a gpr is the intended target of an operation, but an sfr is written to instead. verifying and/or changing the bsr for each read or write to data memory can become very inefficient. to streamline access for the most commonly used data memory locations, the data memory is configured with an access bank, which allows users to access a mapped block of memory without specifying a bsr. the access bank consists of the first 96 bytes of mem- ory (00h-5fh) in bank 0 and the last 160 bytes of mem- ory (60h-ffh) in block 15. the lower half is known as the ?access ram? and is composed of gprs. this upper half is also where the device?s sfrs are mapped. these two areas are mapped contiguously in the access bank and can be addressed in a linear fashion by an 8-bit address ( figure 3-5 and figure 3- 6 ). the access bank is used by core pic18 instructions that include the access ram bit (the ?a? parameter in the instruction). when ?a? is equal to ? 1 ?, the instruction uses the bsr and the 8-bit address included in the opcode for the data memory address. when ?a? is ? 0 ?, however, the instruction is forced to use the access bank address map; the current value of the bsr is ignored entirely. using this ?forced? addressing allows the instruction to operate on a data address in a single cycle, without updating the bsr first. for 8-bit addresses of 60h and above, this means that users can evaluate and operate on sfrs more efficiently. the access ram below 60h is a good place for data values that the user might need to access rapidly, such as immediate computational results or common program variables. access ram also allows for faster and more code efficient context saving and switching of variables. the mapping of the access bank is slightly different when the extended instruction set is enabled (xinst configuration bit = 1 ). this is discussed in more detail in section 3.5.3 ?mapping the access bank in indexed literal offset mode? . 3.3.4 general purpose register file pic18 devices may have banked memory in the gpr area. this is data ram, which is available for use by all instructions. gprs start at the bottom of bank 0 (address 000h) and grow upwards towards the bottom of the sfr area. gprs are not initialized by a power-on reset and are unchanged on all other resets. 3.3.5 special function registers the special function registers (sfrs) are registers used by the cpu and peripheral modules for controlling the desired operation of the device. these registers are implemented as static ram. sfrs start at the top of data memory (fffh) and extend downward to occupy the top portion of bank 15 (f60h to fffh). a list of these registers is given in ta b l e 3 - 1 and tab l e 3 - 2 . the sfrs can be classified into two sets: those associated with the ?core? device functionality (alu, resets and interrupts) and those related to the peripheral functions. the reset and interrupt registers are described in their respective chapters, while the alu?s status register is described later in this section. registers related to the operation of a peripheral feature are described in the chapter for that peripheral. the sfrs are typically distributed among the peripherals whose functions they control. unused sfr locations are unimplemented and read as ? 0 ?s.
pic18f1xk50/pic18lf1xk50 ds41350e-page 40 preliminary ? 2010 microchip technology inc. table 3-1: special function register map for pic18f/lf1xk50 devices address name address name address name address name address name fffh tosu fd7h tmr0h fafh spbrg f87h ? (2) f5fh ueir ffeh tosh fd6h tmr0l faeh rcreg f86h ? (2) f5eh ufrmh ffdh tosl fd5h t0con fadh txreg f85h ? (2) f5dh ufrml ffch stkptr fd4h ? (2) fach txsta f84h ? (2) f5ch uaddr ffbh pclatu fd3h osccon fabh rcsta f83h ? (2) f5bh ueie ffah pclath fd2h osccon2 faah ? f82h portc f5ah uep7 ff9h pcl fd1h wdtcon fa9h eeadr f81h portb f59h uep6 ff8h tblptru fd0h rcon fa8h eedata f80h porta f58h uep5 ff7h tblptrh fcfh tmr1h fa7h eecon2 (1) f7fh anselh f57h uep4 ff6h tblptrl fceh tmr1l fa6h eecon1 f7eh ansel f56h uep3 ff5h tablat fcdh t1con fa5h ? (2) f7dh ? (2) f55h uep2 ff4h prodh fcch tmr2 fa4h ? (2) f7ch ? (2) f54h uep1 ff3h prodl fcbh pr2 fa3h ? (2) f7bh ? (2) f53h uep0 ff2h intcon fcah t2con fa2h ipr2 f7ah iocb ff1h intcon2 fc9h sspbuf fa1h pir2 f79h ioca ff0h intcon3 fc8h sspadd fa0h pie2 f78h wpub fefh indf0 (1) fc7h sspstat f9fh ipr1 f77h wpua feeh postinc0 (1) fc6h sspcon1 f9eh pir1 f76h slrcon fedh postdec0 (1) fc5h sspcon2 f9dh pie1 f75h ? (2) fech preinc0 (1) fc4h adresh f9ch ? (2) f74h ? (2) febh plusw0 (1) fc3h adresl f9bh osctune f73h ? (2) feah fsr0h fc2h adcon0 f9ah ? (2) f72h ? (2) fe9h fsr0l fc1h adcon1 f99h ? (2) f71h ? (2) fe8h wreg fc0h adcon2 f98h ? (2) f70h ? (2) fe7h indf1 (1) fbfh ccpr1h f97h ? (2) f6fh sspmask fe6h postinc1 (1) fbeh ccpr1l f96h ? (2) f6eh ? (2) fe5h postdec1 (1) fbdh ccp1con f95h ? (2) f6dh cm1con0 fe4h preinc1 (1) fbch refcon2 f94h trisc f6ch cm2con1 fe3h plusw1 (1) fbbh refcon1 f93h trisb f6bh cm2con0 fe2h fsr1h fbah refcon0 f92h trisa f6ah ? (2) fe1h fsr1l fb9h pstrcon f91h ? (2) f69h srcon1 fe0h bsr fb8h baudcon f90h ? (2) f68h srcon0 fdfh indf2 (1) fb7h pwm1con f8fh ? (2) f67h ? (2) fdeh postinc2 (1) fb6h eccp1as f8eh ? (2) f66h ? (2) fddh postdec2 (1) fb5h ? (2) f8dh ? (2) f65h ? (2) fdch preinc2 (1) fb4h ? (2) f8ch ? (2) f64h ucon fdbh plusw2 (1) fb3h tmr3h f8bh latc f63h ustat fdah fsr2h fb2h tmr3l f8ah latb f62h uir fd9h fsr2l fb1h t3con f89h lata f61h ucfg fd8h status fb0h spbrgh f88h ? (2) f60h uie note 1: this is not a physical register. 2: unimplemented registers are read as ? 0 ?.
? 2010 microchip technology inc. preliminary ds41350e-page 41 pic18f1xk50/pic18lf1xk50 table 3-2: register file summary (pic18f/lf1xk50) file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor details on page: tosu ? ? ? top-of-stack upper byte (tos<20:16>) ---0 0000 285, 30 tosh top-of-stack, high byte (tos<15:8>) 0000 0000 285, 30 tosl top-of-stack, low byte (tos<7:0>) 0000 0000 285, 30 stkptr stkful stkunf ? sp4 sp3 sp2 sp1 sp0 00-0 0000 285, 31 pclatu ? ? ? holding register for pc<20:16> ---0 0000 285, 30 pclath holding register for pc<15:8> 0000 0000 285, 30 pcl pc, low byte (pc<7:0>) 0000 0000 285, 30 tblptru ? ? ? program memory table pointer upper byte (tblptr<20:16>) ---0 0000 285, 54 tblptrh program memory table pointer, high byte (tblptr<15:8>) 0000 0000 285, 54 tblptrl program memory table pointer, low byte (tblptr<7:0>) 0000 0000 285, 54 tablat program memory table latch 0000 0000 285, 54 prodh product register, high byte xxxx xxxx 285, 65 prodl product register, low byte xxxx xxxx 285, 65 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 0000 000x 285, 70 intcon2 rabpu intedg0 intedg1 intedg2 ?tmr0ip ? rabip 1111 -1-1 285, 71 intcon3 int2ip int1ip ? int2ie int1ie ? int2if int1if 11-0 0-00 285, 72 indf0 uses contents of fsr0 to address data memory ? value of fsr0 not changed (not a physical register) n/a 285, 47 postinc0 uses contents of fsr0 to address data memory ? value of fsr0 post-incremented (not a physical register) n/a 285, 47 postdec0 uses contents of fsr0 to address data memory ? value of fsr0 post-decremented (not a physical register) n/a 285, 47 preinc0 uses contents of fsr0 to address data memory ? value of fsr0 pre-incremented (not a physical register) n/a 285, 47 plusw0 uses contents of fsr0 to address data memory ? value of fsr0 pre-incremented (not a physical register) ? value of fsr0 offset by w n/a 285, 47 fsr0h ? ? ? ? indirect data memory address pointer 0, high byte ---- 0000 285, 47 fsr0l indirect data memory address pointer 0, low byte xxxx xxxx 285, 47 wreg working register xxxx xxxx 285 indf1 uses contents of fsr1 to address data memory ? value of fsr1 not changed (not a physical register) n/a 285, 47 postinc1 uses contents of fsr1 to address data memory ? value of fsr1 post-incremented (not a physical register) n/a 285, 47 postdec1 uses contents of fsr1 to address data memory ? value of fsr1 post-decremented (not a physical register) n/a 285, 47 preinc1 uses contents of fsr1 to address data memory ? value of fsr1 pre-incremented (not a physical register) n/a 285, 47 plusw1 uses contents of fsr1 to address data memory ? value of fsr1 pre-incremented (not a physical register) ? value of fsr1 offset by w n/a 285, 47 fsr1h ? ? ? ? indirect data memory address pointer 1, high byte ---- 0000 286, 47 fsr1l indirect data memory address pointer 1, low byte xxxx xxxx 286, 47 bsr ? ? ? ? bank select register ---- 0000 286, 35 indf2 uses contents of fsr2 to address data memory ? value of fsr2 not changed (not a physical register) n/a 286, 47 postinc2 uses contents of fsr2 to address data memory ? value of fsr2 post-incremented (not a physical register) n/a 286, 47 postdec2 uses contents of fsr2 to address data memory ? value of fsr2 post-decremented (not a physical register) n/a 286, 47 preinc2 uses contents of fsr2 to address data memory ? value of fsr2 pre-incremented (not a physical register) n/a 286, 47 plusw2 uses contents of fsr2 to address data memory ? value of fsr2 pre-incremented (not a physical register) ? value of fsr2 offset by w n/a 286, 47 fsr2h ? ? ? ? indirect data memory address pointer 2, high byte ---- 0000 286, 47 fsr2l indirect data memory address pointer 2, low byte xxxx xxxx 286, 47 status ? ? ?n ov z dcc ---x xxxx 286, 45 legend: x = unknown, u = unchanged, ? = unimplemented, q = value depends on condition note 1: the sboren bit is only available when the boren<1:0> configuration bits = 01 ; otherwise it is disabled and reads as ? 0 ?. see section 23.4 ?brown-out reset (bor)? . 2: the ra3 bit is only available when master clear reset is disabled (mclre configuration bit = 0 ). otherwise, ra3 reads as ? 0 ?. this bit is read-only. 3: bits ra0 and ra1 are available only when usb is disabled.
pic18f1xk50/pic18lf1xk50 ds41350e-page 42 preliminary ? 2010 microchip technology inc. tmr0h timer0 register, high byte 0000 0000 286, 103 tmr0l timer0 register, low byte xxxx xxxx 286, 103 t0con tmr0on t08bit t0cs t0se psa t0ps2 t0ps1 t0ps0 1111 1111 286, 101 osccon idlen ircf2 ircf1 ircf0 osts iosf scs1 scs0 0011 qq00 286, 20 osccon2 ? ? ? ? ? pri_sd hfiofl lfiofs ---- -10x 286, 21 wdtcon ? ? ? ? ? ? ?swdten --- ---0 286, 303 rcon ipen sboren (1) ?ri to pd por bor 0q-1 11q0 277, 284, 79 tmr1h timer1 register, high byte xxxx xxxx 286, 110 tmr1l timer1 register, low bytes xxxx xxxx 286, 110 t1con rd16 t1run t1ckps1 t1ckps0 t1oscen t1sync tmr1cs tmr1on 0000 0000 286, 105 tmr2 timer2 register 0000 0000 286, 112 pr2 timer2 period register 1111 1111 286, 112 t2con ? t2outps3 t2outps2 t2outps1 t2outps0 tmr2on t2ckps1 t2ckps0 -000 0000 286, 111 sspbuf ssp receive buffer/transmit register xxxx xxxx 286, 143, 144 sspadd ssp address register in i 2 c? slave mode. ssp baud rate reload register in i 2 c master mode. 0000 0000 286, 144 sspstat smp cke d/a psr/w ua bf 0000 0000 286, 137, 146 sspcon1 wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 0000 0000 286, 137, 146 sspcon2 gcen ackstat ackdt acken rcen pen rsen sen 0000 0000 286, 147 adresh a/d result register, high byte xxxx xxxx 287, 221 adresl a/d result register, low byte xxxx xxxx 287, 221 adcon0 ? ? chs3 chs2 chs1 chs0 go/done adon --00 0000 287, 215 adcon1 ? ? ? ? pvcfg1 pvcfg0 nvcfg1 nvcfg0 ---- 0000 287, 216 adcon2 adfm ? acqt2 acqt1 acqt0 adcs2 adcs1 adcs0 0-00 0000 287, 217 ccpr1h capture/compare/pwm register 1, high byte xxxx xxxx 287, 138 ccpr1l capture/compare/pwm register 1, low byte xxxx xxxx 287, 138 ccp1con p1m1 p1m0 dc1b1 dc1b0 ccp1m3 ccp1m2 ccp1m1 ccp1m0 0000 0000 287, 117 refcon2 ? ? ? dac1r4 dac1r3 dac1r2 dac1r1 dac1r0 ---0 0000 287, 248 refcon1 d1en d1lps dac1oe --- d1pss1 d1pss0 ? d1nss 000- 00-0 287, 248 refcon0 fvr1en fvr1st fvr1s1 fvr1s0 ? ? ? ? 0001 00-- 287, 247 pstrcon ? ? ? strsync strd strc strb stra ---0 0001 287, 134 baudcon abdovf rcidl dtrxp cktxp brg16 ? wue abden 0100 0-00 287, 192 pwm1con prsen pdc6 pdc5 pdc4 pdc3 pdc2 pdc1 pdc0 0000 0000 287, 133 eccp1as eccpase eccpas2 eccpas1 eccpas0 pssac1 pssac0 pssbd1 pssbd0 0000 0000 287, 129 tmr3h timer3 register, high byte xxxx xxxx 287, 115 tmr3l timer3 register, low byte xxxx xxxx 287, 115 t3con rd16 ? t3ckps1 t3ckps0 t3ccp1 t3sync tmr3cs tmr3on 0-00 0000 287, 113 table 3-2: register file summary (pic18f/lf1xk50) (continued) file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor details on page: legend: x = unknown, u = unchanged, ? = unimplemented, q = value depends on condition note 1: the sboren bit is only available when the boren<1:0> configuration bits = 01 ; otherwise it is disabled and reads as ? 0 ?. see section 23.4 ?brown-out reset (bor)? . 2: the ra3 bit is only available when master clear reset is disabled (mclre configuration bit = 0 ). otherwise, ra3 reads as ? 0 ?. this bit is read-only. 3: bits ra0 and ra1 are available only when usb is disabled.
? 2010 microchip technology inc. preliminary ds41350e-page 43 pic18f1xk50/pic18lf1xk50 spbrgh eusart baud rate generator register, high byte 0000 0000 287, 181 spbrg eusart baud rate generator register, low byte 0000 0000 287, 181 rcreg eusart receive register 0000 0000 287, 182 txreg eusart transmit register 0000 0000 287, 181 txsta csrc tx9 txen sync sendb brgh trmt tx9d 0000 0010 287, 190 rcsta spen rx9 sren cren adden ferr oerr rx9d 0000 000x 287, 191 eeadr eeadr7 eeadr6 eeadr5 eeadr4 eeadr3 eeadr2 eeadr1 eeadr0 0000 0000 287, 52, 61 eedata eeprom data register 0000 0000 287, 52, 61 eecon2 eeprom control register 2 (not a physical register) 0000 0000 287, 52, 61 eecon1 eepgd cfgs ? free wrerr wren wr rd xx-0 x000 287, 53, 61 ipr2 oscfip c1ip c2ip eeip bclip usbip tmr3ip ? 1111 111- 288, 78 pir2 oscfif c1if c2if eeif bclif usbif tmr3if ? 0000 000- 288, 74 pie2 oscfie c1ie c2ie eeie bclie usbie tmr3ie ? 0000 000- 288, 76 ipr1 ? adip rcip txip sspip ccp1ip tmr2ip tmr1ip -111 1111 288, 77 pir1 ? adif rcif txif sspif ccp1if tmr2if tmr1if -000 0000 288, 73 pie1 ? adie rcie txie sspie ccp1ie tmr2ie tmr1ie -000 0000 288, 75 osctune intsrc spllen tun5 tun4 tun3 tun2 tun1 tun0 0000 0000 22, 288 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 1111 1111 288, 94 trisb trisb7 trisb6 trisb5 trisb4 ? ? ? ? 1111 ---- 288, 89 trisa ? ? trisa5 trisa4 ? ? ? ? --11 ---- 288, 83 latc latc7 latc6 latc5 latc4 latc3 latc2 latc1 latc0 xxxx xxxx 288, 94 latb latb7 latb6 latb5 latb4 ? ? ? ? xxxx ---- 288, 89 lata ? ? lata5 lata4 ? ? ? ? --xx ---- 288, 83 portc rc7 rc6 rc5 rc4 rc3 rc2 rc1 rc0 xxxx xxxx 288, 94 portb rb7rb6rb5rb4 ? ? ? ? xxxx ---- 288, 89 porta ? ? ra5 ra4 ra3 (2) ? ra1 (3) ra0 (3) --xx x-xx 288, 83 anselh ? ? ? ? ans11 ans10 ans9 ans8 ---- 1111 288, 99 ansel ans7 ans6 ans5 ans4 ans3 ? ? ? 1111 1--- 288, 98 iocb iocb7 iocb6 iocb5 iocb4 ? ? ? ? 0000 ---- 288, 89 ioca ? ? ioca5 ioca4 ioca3 ? ioca1 ioca0 --00 0-00 288, 83 wpub wpub7 wpub6 wpub5 wpub4 ? ? ? ? 1111 ---- 288, 89 wpua ? ? wpua5 wpua4 wpua3 ? ? ? --11 1--- 285, 89 slrcon ? ? ? ? ? slrc slrb slra ---- -111 288, 100 sspmsk msk7 msk6 msk5 msk4 msk3 msk2 msk1 msk0 1111 1111 288, 154 cm1con0 c1on c1out c1oe c1pol c1sp c1r c1ch1 c1ch0 0000 1000 288, 229 cm2con1 mc1out mc2out c1rsel c2rsel c1hys c2hys c1sync c2sync 0000 0000 288, 230 cm2con0 c2on c2out c2oe c2pol c2sp c2r c2ch1 c2ch0 0000 1000 288, 230 srcon1 srspe srscke srsc2e srsc1e srrpe srrcke srrc2e srrc1e 0000 0000 288, 243 srcon0 srlen srclk2 srclk1 srclk0 srqen srnqen srps srpr 0000 0000 288, 242 ucon ? ppbrst se0 pktdis usben resume suspnd ? -0x0 000- 288, 252 table 3-2: register file summary (pic18f/lf1xk50) (continued) file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor details on page: legend: x = unknown, u = unchanged, ? = unimplemented, q = value depends on condition note 1: the sboren bit is only available when the boren<1:0> configuration bits = 01 ; otherwise it is disabled and reads as ? 0 ?. see section 23.4 ?brown-out reset (bor)? . 2: the ra3 bit is only available when master clear reset is disabled (mclre configuration bit = 0 ). otherwise, ra3 reads as ? 0 ?. this bit is read-only. 3: bits ra0 and ra1 are available only when usb is disabled.
pic18f1xk50/pic18lf1xk50 ds41350e-page 44 preliminary ? 2010 microchip technology inc. ustat ? endp3 endp2 endp1 endp0 dir ppbi ? -xxx xxx- 289, 256 uir ? sofif stallif idleif trnif actvif uerrif urstif -000 0000 289, 266 ucfg uteye ? ? upuen ? fsen ppb1 ppb0 0--0 -000 289, 254 uie ? sofie stallie idleie trnie actvie uerrie urstie -000 0000 289, 268 ueir btsef ? ? btoef dfn8ef crc16ef crc5ef pidef 0--0 0000 289, 269 ufrmh ? ? ? ? ? frm10 frm9 frm8 ---- -xxx 289, 252 ufrml frm7 frm6 frm5 frm4 frm3 frm2 frm1 frm0 xxxx xxxx 289, 252 uaddr ? addr6 addr5 addr4 addr3 addr2 addr1 addr0 -000 0000 289, 258 ueie btsee ? ? btoee dfn8ee crc16ee crc5ee pidee 0--0 0000 289, 270 uep7 ? ? ? ephshk epcondis epouten epinen epstall ---0 0000 289, 257 uep6 ? ? ? ephshk epcondis epouten epinen epstall ---0 0000 289, 257 uep5 ? ? ? ephshk epcondis epouten epinen epstall ---0 0000 289, 257 uep4 ? ? ? ephshk epcondis epouten epinen epstall ---0 0000 289, 257 uep3 ? ? ? ephshk epcondis epouten epinen epstall ---0 0000 289, 257 uep2 ? ? ? ephshk epcondis epouten epinen epstall ---0 0000 289, 257 uep1 ? ? ? ephshk epcondis epouten epinen epstall ---0 0000 289, 257 uep0 ? ? ? ephshk epcondis epouten epinen epstall ---0 0000 285, 257 table 3-2: register file summary (pic18f/lf1xk50) (continued) file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor details on page: legend: x = unknown, u = unchanged, ? = unimplemented, q = value depends on condition note 1: the sboren bit is only available when the boren<1:0> configuration bits = 01 ; otherwise it is disabled and reads as ? 0 ?. see section 23.4 ?brown-out reset (bor)? . 2: the ra3 bit is only available when master clear reset is disabled (mclre configuration bit = 0 ). otherwise, ra3 reads as ? 0 ?. this bit is read-only. 3: bits ra0 and ra1 are available only when usb is disabled.
? 2010 microchip technology inc. preliminary ds41350e-page 45 pic18f1xk50/pic18lf1xk50 3.3.6 status register the status register, shown in register 3-2 , contains the arithmetic status of the alu. as with any other sfr, it can be the operand for any instruction. if the status register is the destination for an instruc- tion that affects the z, dc, c, ov or n bits, the results of the instruction are not written; instead, the status register is updated according to the instruction per- formed. therefore, the result of an instruction with the status register as its destination may be different than intended. as an example, clrf status will set the z bit and leave the remaining status bits unchanged (? 000u u1uu ?). it is recommended that only bcf , bsf , swapf , movff and movwf instructions are used to alter the status register, because these instructions do not affect the z, c, dc, ov or n bits in the status register. for other instructions that do not affect status bits, see the instruction set summaries in table 25-2 and table 25-3 . note: the c and dc bits operate as the borrow and digit borrow bits, respectively, in subtraction. register 3-2: status: status register u-0 u-0 u-0 r/w-x r/w-x r/w-x r/w-x r/w-x ? ? ? nov z dc (1) c (1) bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-5 unimplemented: read as ? 0 ? bit 4 n: negative bit this bit is used for signed arithmetic (two?s complement). it indicates whether the result was negative (alu msb = 1 ). 1 = result was negative 0 = result was positive bit 3 ov: overflow bit this bit is used for signed arithmetic (two?s complement). it indicates an overflow of the 7-bit magni- tude which causes the sign bit (bit 7 of the result) to change state. 1 = overflow occurred for signed arithmetic (in this arithmetic operation) 0 = no overflow occurred bit 2 z: zero bit 1 = the result of an arithmetic or logic operation is zero 0 = the result of an arithmetic or logic operation is not zero bit 1 dc: digit carry/borrow bit ( addwf , addlw,sublw,subwf instructions) (1) 1 = a carry-out from the 4th low-order bit of the result occurred 0 = no carry-out from the 4th low-order bit of the result bit 0 c: carry/borrow bit ( addwf , addlw, sublw, subwf instructions) (1) 1 = a carry-out from the most significant bit of the result occurred 0 = no carry-out from the most significant bit of the result occurred note 1: for borrow , the polarity is reversed. a subtraction is executed by adding the two?s complement of the second operand. for rotate ( rrf , rlf ) instructions, this bit is loaded with either the high-order or low-order bit of the source register.
pic18f1xk50/pic18lf1xk50 ds41350e-page 46 preliminary ? 2010 microchip technology inc. 3.4 data addressing modes while the program memory can be addressed in only one way ? through the program counter ? information in the data memory space can be addressed in several ways. for most instructions, the addressing mode is fixed. other instructions may use up to three modes, depending on which operands are used and whether or not the extended instruction set is enabled. the addressing modes are: ? inherent ? literal ?direct ?indirect an additional addressing mode, indexed literal offset, is available when the extended instruction set is enabled (xinst configuration bit = 1 ). its operation is discussed in greater detail in section 3.5.1 ?indexed addressing with literal offset? . 3.4.1 inherent and literal addressing many pic18 control instructions do not need any argu- ment at all; they either perform an operation that glob- ally affects the device or they operate implicitly on one register. this addressing mode is known as inherent addressing. examples include sleep , reset and daw . other instructions work in a similar way but require an additional explicit argument in the opcode. this is known as literal addressing mode because they require some literal value as an argument. examples include addlw and movlw , which respectively, add or move a literal value to the w register. other examples include call and goto , which include a 20-bit program memory address. 3.4.2 direct addressing direct addressing specifies all or part of the source and/or destination address of the operation within the opcode itself. the options are specified by the arguments accompanying the instruction. in the core pic18 instruction set, bit-oriented and byte- oriented instructions use some version of direct addressing by default. all of these instructions include some 8-bit literal address as their least significant byte. this address specifies either a register address in one of the banks of data ram ( section 3.3.4 ?general purpose register file? ) or a location in the access bank ( section 3.3.3 ?access bank? ) as the data source for the instruction. the access ram bit ?a? determines how the address is interpreted. when ?a? is ? 1 ?, the contents of the bsr ( section 3.3.2 ?bank select register (bsr)? ) are used with the address to determine the complete 12-bit address of the register. when ?a? is ? 0 ?, the address is interpreted as being a register in the access bank. addressing that uses the access ram is sometimes also known as direct forced addressing mode. a few instructions, such as movff , include the entire 12-bit address (either source or destination) in their opcodes. in these cases, the bsr is ignored entirely. the destination of the operation?s results is determined by the destination bit ?d?. when ?d? is ? 1 ?, the results are stored back in the source register, overwriting its origi- nal contents. when ?d? is ? 0 ?, the results are stored in the w register. instructions without the ?d? argument have a destination that is implicit in the instruction; their destination is either the target register being operated on or the w register. 3.4.3 indirect addressing indirect addressing allows the user to access a location in data memory without giving a fixed address in the instruction. this is done by using file select registers (fsrs) as pointers to the locations which are to be read or written. since the fsrs are themselves located in ram as special file registers, they can also be directly manipulated under program control. this makes fsrs very useful in implementing data struc- tures, such as tables and arrays in data memory. the registers for indirect addressing are also implemented with indirect file operands (indfs) that permit automatic manipulation of the pointer value with auto-incrementing, auto-decrementing or offsetting with another value. this allows for efficient code, using loops, such as the example of clearing an entire ram bank in example 3-5 . example 3-5: how to clear ram (bank 1) using indirect addressing note: the execution of some instructions in the core pic18 instruction set are changed when the pic18 extended instruction set is enabled. see section 3.5 ?data memory and the extended instruction set? for more information. lfsr fsr0, 100h ; next clrf postinc0 ; clear indf ; register then ; inc pointer btfss fsr0h, 1 ; all done with ; bank1? bra next ; no, clear next continue ; yes, continue
? 2010 microchip technology inc. preliminary ds41350e-page 47 pic18f1xk50/pic18lf1xk50 3.4.3.1 fsr registers and the indf operand at the core of indirect addressing are three sets of reg- isters: fsr0, fsr1 and fsr2. each represents a pair of 8-bit registers, fsrnh and fsrnl. each fsr pair holds a 12-bit value, therefore the four upper bits of the fsrnh register are not used. the 12-bit fsr value can address the entire range of the data memory in a linear fashion. the fsr register pairs, then, serve as pointers to data memory locations. indirect addressing is accomplished with a set of indirect file operands, indf0 through indf2. these can be thought of as ?virtual? registers: they are mapped in the sfr space but are not physically implemented. reading or writing to a particular indf register actually accesses its corresponding fsr register pair. a read from indf1, for example, reads the data at the address indicated by fsr1h:fsr1l. instructions that use the indf registers as operands actually use the contents of their corresponding fsr as a pointer to the instruction?s target. the indf operand is just a convenient way of using the pointer. because indirect addressing uses a full 12-bit address, data ram banking is not necessary. thus, the current contents of the bsr and the access ram bit have no effect on determining the target address. 3.4.3.2 fsr registers and postinc, postdec, preinc and plusw in addition to the indf operand, each fsr register pair also has four additional indirect operands. like indf, these are ?virtual? registers which cannot be directly read or written. accessing these registers actually accesses the location to which the associated fsr register pair points, and also performs a specific action on the fsr value. they are: ? postdec: accesses the location to which the fsr points, then automatically decrements the fsr by 1 afterwards ? postinc: accesses the location to which the fsr points, then automatically increments the fsr by 1 afterwards ? preinc: automatically increments the fsr by 1, then uses the location to which the fsr points in the operation ? plusw: adds the signed value of the w register (range of -127 to 128) to that of the fsr and uses the location to which the result points in the operation. in this context, accessing an indf register uses the value in the associated fsr register without changing it. similarly, accessing a plusw register gives the fsr value an offset by that in the w register; however, neither w nor the fsr is actually changed in the operation. accessing the other virtual registers changes the value of the fsr register. figure 3-8: indirect addressing fsr1h:fsr1l 0 7 data memory 000h 100h 200h 300h f00h e00h fffh bank 0 bank 1 bank 2 bank 14 bank 15 bank 3 through bank 13 addwf, indf1, 1 0 7 using an instruction with one of the indirect addressing registers as the operand.... ...uses the 12-bit address stored in the fsr pair associated with that register.... ...to determine the data memory location to be used in that operation. in this case, the fsr1 pair contains ecch. this means the contents of location ecch will be added to that of the w register and stored back in ecch. xxxx 1110 11001100
pic18f1xk50/pic18lf1xk50 ds41350e-page 48 preliminary ? 2010 microchip technology inc. operations on the fsrs with postdec, postinc and preinc affect the entire register pair; that is, roll- overs of the fsrnl register from ffh to 00h carry over to the fsrnh register. on the other hand, results of these operations do not change the value of any flags in the status register (e.g., z, n, ov, etc.). the plusw register can be used to implement a form of indexed addressing in the data memory space. by manipulating the value in the w register, users can reach addresses that are fixed offsets from pointer addresses. in some applications, this can be used to implement some powerful program control structure, such as software stacks, inside of data memory. 3.4.3.3 operations by fsrs on fsrs indirect addressing operations that target other fsrs or virtual registers represent special cases. for example, using an fsr to point to one of the virtual registers will not result in successful operations. as a specific case, assume that fsr0h:fsr0l contains fe7h, the address of indf1. attempts to read the value of the indf1 using indf0 as an operand will return 00h. attempts to write to indf1 using indf0 as the operand will result in a nop . on the other hand, using the virtual registers to write to an fsr pair may not occur as planned. in these cases, the value will be written to the fsr pair but without any incrementing or decrementing. thus, writing to either the indf2 or postdec2 register will write the same value to the fsr2h:fsr2l. since the fsrs are physical registers mapped in the sfr space, they can be manipulated through all direct operations. users should proceed cautiously when working on these registers, particularly if their code uses indirect addressing. similarly, operations by indirect addressing are generally permitted on all other sfrs. users should exercise the appropriate caution that they do not inadvertently change settings that might affect the operation of the device. 3.5 data memory and the extended instruction set enabling the pic18 extended instruction set (xinst configuration bit = 1 ) significantly changes certain aspects of data memory and its addressing. specifi- cally, the use of the access bank for many of the core pic18 instructions is different; this is due to the intro- duction of a new addressing mode for the data memory space. what does not change is just as important. the size of the data memory space is unchanged, as well as its linear addressing. the sfr map remains the same. core pic18 instructions can still operate in both direct and indirect addressing mode; inherent and literal instructions do not change at all. indirect addressing with fsr0 and fsr1 also remain unchanged. 3.5.1 indexed addressing with literal offset enabling the pic18 extended instruction set changes the behavior of indirect addressing using the fsr2 register pair within access ram. under the proper conditions, instructions that use the access bank ? that is, most bit-oriented and byte-oriented instructions ? can invoke a form of indexed addressing using an offset specified in the instruction. this special addressing mode is known as indexed addressing with literal offset, or indexed literal offset mode. when using the extended instruction set, this addressing mode requires the following: ? the use of the access bank is forced (?a? = 0 ) and ? the file address argument is less than or equal to 5fh. under these conditions, the file address of the instruction is not interpreted as the lower byte of an address (used with the bsr in direct addressing), or as an 8-bit address in the access bank. instead, the value is interpreted as an offset value to an address pointer, specified by fsr2. the offset and the contents of fsr2 are added to obtain the target address of the operation. 3.5.2 instructions affected by indexed literal offset mode any of the core pic18 instructions that can use direct addressing are potentially affected by the indexed literal offset addressing mode. this includes all byte-oriented and bit-oriented instructions, or almost one-half of the standard pic18 instruction set. instructions that only use inherent or literal addressing modes are unaffected. additionally, byte-oriented and bit-oriented instructions are not affected if they do not use the access bank (access ram bit is ? 1 ?), or include a file address of 60h or above. instructions meeting these criteria will continue to execute as before. a comparison of the different possible addressing modes when the extended instruction set is enabled is shown in figure 3-9 . those who desire to use byte-oriented or bit-oriented instructions in the indexed literal offset mode should note the changes to assembler syntax for this mode. this is described in more detail in section 25.2.1 ?extended instruction syntax? .
? 2010 microchip technology inc. preliminary ds41350e-page 49 pic18f1xk50/pic18lf1xk50 figure 3-9: comparing addressing options for bit-oriented and byte-oriented instructions (extended instruction set enabled) example instruction: addwf, f, d, a (opcode: 0010 01da ffff ffff ) when ?a? = 0 and f ? 60h: the instruction executes in direct forced mode. ?f? is inter- preted as a location in the access ram between 060h and 0ffh. this is the same as locations f60h to fffh (bank 15) of data memory. locations below 60h are not available in this addressing mode. when ?a? = 0 and f ??? 5fh: the instruction executes in indexed literal offset mode. ?f? is interpreted as an offset to the address value in fsr2. the two are added together to obtain the address of the target register for the instruction. the address can be anywhere in the data memory space. note that in this mode, the correct syntax is now: addwf [k], d where ?k? is the same as ?f?. when ?a? = 1 (all values of f): the instruction executes in direct mode (also known as direct long mode). ?f? is inter- preted as a location in one of the 16 banks of the data memory space. the bank is designated by the bank select register (bsr). the address can be in any implemented bank in the data memory space. 000h 060h 100h f00h f60h fffh valid range 00h 60h ffh data memory access ram bank 0 bank 1 through bank 14 bank 15 sfrs 000h 060h 100h f00h f60h fffh data memory bank 0 bank 1 through bank 14 bank 15 sfrs fsr2h fsr2l ffffffff 001001da ffffffff 001001da 000h 060h 100h f00h f60h fffh data memory bank 0 bank 1 through bank 14 bank 15 sfrs for ?f? bsr 00000000
pic18f1xk50/pic18lf1xk50 ds41350e-page 50 preliminary ? 2010 microchip technology inc. 3.5.3 mapping the access bank in indexed literal offset mode the use of indexed literal offset addressing mode effectively changes how the first 96 locations of access ram (00h to 5fh) are mapped. rather than containing just the contents of the bottom section of bank 0, this mode maps the contents from a user defined ?window? that can be located anywhere in the data memory space. the value of fsr2 establishes the lower bound- ary of the addresses mapped into the window, while the upper boundary is defined by fsr2 plus 95 (5fh). addresses in the access ram above 5fh are mapped as previously described (see section 3.3.3 ?access bank? ). an example of access bank remapping in this addressing mode is shown in figure 3-10 . remapping of the access bank applies only to opera- tions using the indexed literal offset mode. operations that use the bsr (access ram bit is ? 1 ?) will continue to use direct addressing as before. 3.6 pic18 instruction execution and the extended instruction set enabling the extended instruction set adds eight additional commands to the existing pic18 instruction set. these instructions are executed as described in section 25.2 ?extended instruction set? . figure 3-10: remapping the access bank with indexed literal offset addressing data memory 000h 100h 200h f60h f00h fffh bank 1 bank 15 bank 2 through bank 14 sfrs addwf f, d, a fsr2h:fsr2l = 120h locations in the region from the fsr2 pointer (120h) to the pointer plus 05fh (17fh) are mapped to the bottom of the access ram (000h-05fh). special file registers at f60h through fffh are mapped to 60h through ffh, as usual. bank 0 addresses below 5fh can still be addressed by using the bsr. access bank 00h 60h ffh sfrs bank 1 ?window? bank 0 window example situation: 120h 17fh 5fh bank 1
? 2010 microchip technology inc. preliminary ds41350e-page 51 pic18f1xk50/pic18lf1xk50 4.0 flash program memory the flash program memory is readable, writable and erasable during normal operation over the entire v dd range. a read from program memory is executed one byte at a time. a write to program memory is executed on blocks of 16 or 8 bytes at a time depending on the spe- cific device (see table 4-1 ). program memory is erased in blocks of 64 bytes at a time. the difference between the write and erase block sizes requires from 1 to 8 block writes to restore the contents of a single block erase. a bulk erase operation can not be issued from user code. table 4-1: write/erase block sizes writing or erasing program memory will cease instruction fetches until the operation is complete. the program memory cannot be accessed during the write or erase, therefore, code cannot execute. an internal programming timer terminates program memory writes and erases. a value written to program memory does not need to be a valid instruction. executing a program memory location that forms an invalid instruction results in a nop . 4.1 table reads and table writes in order to read and write program memory, there are two operations that allow the processor to move bytes between the program memory space and the data ram: ? table read ( tblrd ) ? table write ( tblwt ) the program memory space is 16 bits wide, while the data ram space is 8 bits wide. table reads and table writes move data between these two memory spaces through an 8-bit register (tablat). the table read operation retrieves one byte of data directly from program memory and places it into the tablat register. figure 4-1 shows the operation of a table read. the table write operation stores one byte of data from the tablat register into a write block holding register. the procedure to write the contents of the holding registers into program memory is detailed in section 4.5 ?writing to flash program memory? . figure 4-2 shows the operation of a table write with program memory and data ram. table operations work with byte entities. tables contain- ing data, rather than program instructions, are not required to be word aligned. t herefore, a table can start and end at any byte address. if a table write is being used to write executable code into program memory, program instructions will need to be word aligned. figure 4-1: table read operation device write block size (bytes) erase block size (bytes) pic18f13k50 8 64 pic18f14k50 16 64 table pointer (1) table latch (8-bit) program memory tblptrh tblptrl tablat tblptru instruction: tblrd * note 1: table pointer register points to a byte in program memory. program memory (tblptr)
pic18f1xk50/pic18lf1xk50 ds41350e-page 52 preliminary ? 2010 microchip technology inc. figure 4-2: table write operation 4.2 control registers several control registers are used in conjunction with the tblrd and tblwt instructions. these include the: ? eecon1 register ? eecon2 register ? tablat register ? tblptr registers 4.2.1 eecon1 and eecon2 registers the eecon1 register ( register 4-1 ) is the control register for memory accesses. the eecon2 register is not a physical register; it is used exclusively in the memory write and erase sequences. reading eecon2 will read all ? 0 ?s. the eepgd control bit determines if the access will be a program or data eeprom memory access. when eepgd is clear, any subsequent operations will operate on the data eeprom memory. when eepgd is set, any subsequent operations will operate on the program memory. the cfgs control bit determines if the access will be to the configuration/calibration registers or to program memory/data eeprom memory. when cfgs is set, subsequent operations will operate on configuration registers regardless of eepgd (see section 24.0 ?special features of the cpu? ). when cfgs is clear, memory selection access is determined by eepgd. the free bit allows the program memory erase oper- ation. when free is set, an erase operation is initiated on the next wr command. when free is clear, only writes are enabled. the wren bit, when set, will allow a write operation. the wren bit is clear on power-up. the wrerr bit is set by hardware when the wr bit is set and cleared when the internal programming timer expires and the write operation is complete. the wr control bit initiates write operations. the wr bit cannot be cleared, only set, by firmware. then wr bit is cleared by hardware at the completion of the write operation. table pointer (1) table latch (8-bit) tblptrh tblptrl tablat program memory (tblptr) tblptru instruction: tblwt * note 1: during table writes the table pointer does not point directly to program memory. the lsbs of tblprtl actually point to an address within the write block hol ding registers. the msbs of the table pointer deter- mine where the write block will eventually be written. t he process for writing the holding registers to the program memory array is discussed in section 4.5 ?writing to flash program memory? . holding registers program memory note: during normal operation, the wrerr is read as ? 1 ?. this can indicate that a write operation was prematurely terminated by a reset, or a write operation was attempted improperly. note: the eeif interrupt flag bit of the pir2 register is set when the write is complete. the eeif flag stays set until cleared by firmware.
? 2010 microchip technology inc. preliminary ds41350e-page 53 pic18f1xk50/pic18lf1xk50 register 4-1: eecon1: data eeprom control 1 register r/w-x r/w-x u-0 r/w-0 r/w-x r/w-0 r/s-0 r/s-0 eepgd cfgs ? free wrerr wren wr rd bit 7 bit 0 legend: r = readable bit w = writable bit s = bit can be set by software, but not cleared u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 eepgd: flash program or data eeprom memory select bit 1 = access flash program memory 0 = access data eeprom memory bit 6 cfgs: flash program/data eeprom or configuration select bit 1 = access configuration registers 0 = access flash program or data eeprom memory bit 5 unimplemented: read as ? 0 ? bit 4 free: flash row (block) erase enable bit 1 = erase the program memory block addressed by tblptr on the next wr command (cleared by completion of erase operation) 0 = perform write-only bit 3 wrerr: flash program/data eeprom error flag bit (1) 1 = a write operation is prematurely terminated (any reset during self-timed programming in normal operation, or an improper write attempt) 0 = the write operation completed bit 2 wren: flash program/data eeprom write enable bit 1 = allows write cycles to flash program/data eeprom 0 = inhibits write cycles to flash program/data eeprom bit 1 wr: write control bit 1 = initiates a data eeprom erase/write cycle or a program memory erase cycle or write cycle. (the operation is self-timed and the bit is cleared by hardware once write is complete. the wr bit can only be set (not cleared) by software.) 0 = write cycle to the eeprom is complete bit 0 rd: read control bit 1 = initiates an eeprom read (read takes one cycle. rd is cleared by hardware. the rd bit can only be set (not cleared) by software. rd bit cannot be set when eepgd = 1 or cfgs = 1 .) 0 = does not initiate an eeprom read note 1: when a wrerr occurs, the eepgd and cfgs bits are not cleared. this allows tracing of the error condition.
pic18f1xk50/pic18lf1xk50 ds41350e-page 54 preliminary ? 2010 microchip technology inc. 4.2.2 tablat ? table latch register the table latch (tablat) is an 8-bit register mapped into the sfr space. the table latch register is used to hold 8-bit data during data transfers between program memory and data ram. 4.2.3 tblptr ? table pointer register the table pointer (tblptr) register addresses a byte within the program memory. the tblptr is comprised of three sfr registers: table pointer upper byte, table pointer high byte and table pointer low byte (tblptru:tblptrh:tblptrl). these three regis- ters join to form a 22-bit wide pointer. the low-order 21 bits allow the device to address up to 2 mbytes of program memory space. the 22nd bit allows access to the device id, the user id and the configuration bits. the table pointer register, tblptr, is used by the tblrd and tblwt instructions. these instructions can update the tblptr in one of four ways based on the table operation. these operations are shown in table 4-2 . these operations on the tblptr affect only the low-order 21 bits. 4.2.4 table pointer boundaries tblptr is used in reads, writes and erases of the flash program memory. when a tblrd is executed, all 22 bits of the tblptr determine which byte is read from program memory directly into the tablat register. when a tblwt is executed the byte in the tablat reg- ister is written, not to flash memory but, to a holding register in preparation for a program memory write. the holding registers constitute a write block which varies depending on the device (see tab l e 4 - 1 ).the 3, 4, or 5 lsbs of the tblptrl register determine which specific address within the holding register block is written to. the msbs of the table pointer have no effect during tblwt operations. when a program memory write is executed the entire holding register block is written to the flash memory at the address determined by the msbs of the tblptr. the 3, 4, or 5 lsbs are ignored during flash memory writes. for more detail, see section 4.5 ?writing to flash program memory? . when an erase of program memory is executed, the 16 msbs of the table pointer register (tblptr<21:6>) point to the 64-byte block that will be erased. the least significant bits (tblptr<5:0>) are ignored. figure 4-3 describes the relevant boundaries of tblptr based on flash program memory operations. table 4-2: table pointer operations with tblrd and tblwt instructions figure 4-3: table pointer bo undaries based on operation example operation on table pointer tblrd* tblwt* tblptr is not modified tblrd*+ tblwt*+ tblptr is incremented after the read/write tblrd*- tblwt*- tblptr is decremented after the read/write tblrd+* tblwt+* tblptr is incremented before the read/write 21 16 15 87 0 table erase/write table write table read ? tblptr<21:0> tblptrl tblptrh tblptru tblptr (1) tblptr<21:n+1> (1) note 1: n = 3, 4, 5, or 6 for block sizes of 8, 16, 32 or 64 bytes, respectively.
? 2010 microchip technology inc. preliminary ds41350e-page 55 pic18f1xk50/pic18lf1xk50 4.3 reading the flash program memory the tblrd instruction retrieves data from program memory and places it into data ram. table reads from program memory are performed one byte at a time. tblptr points to a byte address in program space. executing tblrd places the byte pointed to into tablat. in addition, tblptr can be modified automatically for the next table read operation. the internal program memory is typically organized by words. the least significant bit of the address selects between the high and low bytes of the word. figure 4-4 shows the interface between the internal program memory and the tablat. figure 4-4: reads from flash program memory example 4-1: reading a flash program memory word (even byte address) program memory (odd byte address) tblrd tablat tblptr = xxxxx1 fetch instruction register (ir) read register tblptr = xxxxx0 movlw code_addr_upper ; load tblptr with the base movwf tblptru ; address of the word movlw code_addr_high movwf tblptrh movlw code_addr_low movwf tblptrl read_word tblrd*+ ; read into tablat and increment movf tablat, w ; get data movwf word_even tblrd*+ ; read into tablat and increment movfw tablat, w ; get data movf word_odd
pic18f1xk50/pic18lf1xk50 ds41350e-page 56 preliminary ? 2010 microchip technology inc. 4.4 erasing flash program memory the minimum erase block is 32 words or 64 bytes. only through the use of an external programmer, or through icsp? control, can larger blocks of program memory be bulk erased. word erase in the flash array is not supported. when initiating an erase sequence from the microcon- troller itself, a block of 64 bytes of program memory is erased. the most significant 16 bits of the tblptr<21:6> point to the block being erased. the tblptr<5:0> bits are ignored. the eecon1 register commands the erase operation. the eepgd bit must be set to point to the flash pro- gram memory. the wren bit must be set to enable write operations. the free bit is set to select an erase operation. the write initiate sequence for eecon2, shown as steps 4 through 6 in section 4.4.1 ?flash program memory erase sequence? , is used to guard against accidental writes. this is sometimes referred to as a long write. a long write is necessary for erasing the internal flash. instruction execution is halted during the long write cycle. the long write is terminated by the internal programming timer. 4.4.1 flash program memory erase sequence the sequence of events for erasing a block of internal program memory is: 1. load table pointer register with address of block being erased. 2. set the eecon1 register for the erase operation: ? set eepgd bit to point to program memory; ? clear the cfgs bit to access program memory; ? set wren bit to enable writes; ? set free bit to enable the erase. 3. disable interrupts. 4. write 55h to eecon2. 5. write 0aah to eecon2. 6. set the wr bit. this will begin the block erase cycle. 7. the cpu will stall for duration of the erase (about 2 ms using internal timer). 8. re-enable interrupts. example 4-2: erasing a flash program memory block movlw code_addr_upper ; load tblptr with the base movwf tblptru ; address of the memory block movlw code_addr_high movwf tblptrh movlw code_addr_low movwf tblptrl erase_block bsf eecon1, eepgd ; point to flash program memory bcf eecon1, cfgs ; access flash program memory bsf eecon1, wren ; enable write to memory bsf eecon1, free ; enable block erase operation bcf intcon, gie ; disable interrupts required movlw 55h sequence movwf eecon2 ; write 55h movlw 0aah movwf eecon2 ; write 0aah bsf eecon1, wr ; start erase (cpu stall) bsf intcon, gie ; re-enable interrupts
? 2010 microchip technology inc. preliminary ds41350e-page 57 pic18f1xk50/pic18lf1xk50 4.5 writing to flash program memory the programming block size is 8 or 16 bytes, depending on the device (see ta b l e 4 - 1 ). word or byte programming is not supported. table writes are used internally to load the holding registers needed to program the flash memory. there are only as many holding registers as there are bytes in a write block (see table 4-1 ). since the table latch (tablat) is only a single byte, the tblwt instruction may need to be executed 8, or 16 times, depending on the device, for each programming operation. all of the table write operations will essen- tially be short writes because only the holding registers are written. after all the holding registers have been written, the programming operation of that block of memory is started by configuring the eecon1 register for a program memory write and performing the long write sequence. the long write is necessary for programming the inter- nal flash. instruction execution is halted during a long write cycle. the long write will be terminated by the internal programming timer. the eeprom on-chip timer controls the write time. the write/erase voltages are generated by an on-chip charge pump, rated to operate over the voltage range of the device. figure 4-5: table writes to flash program memory 4.5.1 flash program memory write sequence the sequence of events for programming an internal program memory location should be: 1. read 64 bytes into ram. 2. update data values in ram as necessary. 3. load table pointer register with address being erased. 4. execute the block erase procedure. 5. load table pointer register with address of first byte being written. 6. write the 8 or 16-byte block into the holding registers with auto-increment. 7. set the eecon1 register for the write operation: ? set eepgd bit to point to program memory; ? clear the cfgs bit to access program memory; ? set wren to enable byte writes. 8. disable interrupts. 9. write 55h to eecon2. 10. write 0aah to eecon2. 11. set the wr bit. this will begin the write cycle. 12. the cpu will stall for duration of the write (about 2 ms using internal timer). 13. re-enable interrupts. 14. repeat steps 6 to 13 for each block until all 64 bytes are written. 15. verify the memory (table read). this procedure will require about 6 ms to update each write block of memory. an example of the required code is given in example 4-3 . note: the default value of the holding registers on device resets and after write operations is ffh. a write of ffh to a holding register does not modify that byte. this means that individual bytes of program memory may be modified, provided that the change does not attempt to change any bit from a ? 0 ? to a ? 1 ?. when modifying individual bytes, it is not necessary to load all holding registers before executing a long write operation. tablat tblptr = xxxxyy (1) tblptr = xxxx01 tblptr = xxxx00 write register tblptr = xxxx02 program memory holding register holding register holding register holding register 8 8 8 8 note 1: yy = x7, xf, or 1f for 8, 16 or 32 byte write blocks, respectively. note: before setting the wr bit, the table pointer address needs to be within the intended address range of the bytes in the holding registers.
pic18f1xk50/pic18lf1xk50 ds41350e-page 58 preliminary ? 2010 microchip technology inc. example 4-3: writing to flash program memory movlw d'64? ; number of bytes in erase block movwf counter movlw buffer_addr_high ; point to buffer movwf fsr0h movlw buffer_addr_low movwf fsr0l movlw code_addr_upper ; load tblptr with the base movwf tblptru ; address of the memory block movlw code_addr_high movwf tblptrh movlw code_addr_low movwf tblptrl read_block tblrd*+ ; read into tablat, and inc movf tablat, w ; get data movwf postinc0 ; store data decfsz counter ; done? bra read_block ; repeat modify_word movlw buffer_addr_high ; point to buffer movwf fsr0h movlw buffer_addr_low movwf fsr0l movlw new_data_low ; update buffer word movwf postinc0 movlw new_data_high movwf indf0 erase_block movlw code_addr_upper ; load tblptr with the base movwf tblptru ; address of the memory block movlw code_addr_high movwf tblptrh movlw code_addr_low movwf tblptrl bsf eecon1, eepgd ; point to flash program memory bcf eecon1, cfgs ; access flash program memory bsf eecon1, wren ; enable write to memory bsf eecon1, free ; enable erase operation bcf intcon, gie ; disable interrupts movlw 55h required movwf eecon2 ; write 55h sequence movlw 0aah movwf eecon2 ; write 0aah bsf eecon1, wr ; start erase (cpu stall) bsf intcon, gie ; re-enable interrupts tblrd*- ; dummy read decrement movlw buffer_addr_high ; point to buffer movwf fsr0h movlw buffer_addr_low movwf fsr0l write_buffer_back movlw blocksize ; number of bytes in holding register movwf counter movlw d?64?/blocksize ; number of write blocks in 64 bytes movwf counter2 write_byte_to_hregs movf postinc0, w ; get low byte of buffer data movwf tablat ; present data to table latch tblwt+* ; write data, perform a short write ; to internal tblwt holding register.
? 2010 microchip technology inc. preliminary ds41350e-page 59 pic18f1xk50/pic18lf1xk50 example 4-3: writing to flash program memory (continued) 4.5.2 write verify depending on the application, good programming practice may dictate that the value written to the memory should be verified against the original value. this should be used in applications where excessive writes can stress bits near the specification limit. 4.5.3 unexpected termination of write operation if a write is terminated by an unplanned event, such as loss of power or an unexpected reset, the memory location just programmed should be verified and reprogrammed if needed. if the write operation is interrupted by a mclr reset or a wdt time-out reset during normal operation, the wrerr bit will be set which the user can check to decide whether a rewrite of the location(s) is needed. 4.5.4 protection against spurious writes to protect against spurious writes to flash program memory, the write initiate sequence must also be followed. see section 24.0 ?special features of the cpu? for more detail. 4.6 flash program operation during code protection see section 24.3 ?program verification and code protection? for details on code protection of flash program memory. table 4-3: registers associated with program flash memory decfsz counter ; loop until holding registers are full bra write_word_to_hregs program_memory bsf eecon1, eepgd ; point to flash program memory bcf eecon1, cfgs ; access flash program memory bsf eecon1, wren ; enable write to memory bcf intcon, gie ; disable interrupts movlw 55h required movwf eecon2 ; write 55h sequence movlw 0aah movwf eecon2 ; write 0aah bsf eecon1, wr ; start program (cpu stall) dcfsz counter2 ; repeat for remaining write blocks bra write_byte_to_hregs ; bsf intcon, gie ; re-enable interrupts bcf eecon1, wren ; disable write to memory name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page tblptru ? ? bit 21 program memory table pointer upper byte (tblptr<20:16>) 285 tbpltrh program memory table pointer high byte (tblptr<15:8>) 285 tblptrl program memory table pointer low byte (tblptr<7:0>) 285 tablat program memory table latch 285 intcon gie/gieh peie/giel tmr0ie in t0ie rabie tmr0if int0if rabif 285 eecon2 eeprom control register 2 (not a physical register) 287 eecon1 eepgd cfgs ? free wrerr wren wr rd 287 ipr2 oscfip c1ip c2ip eeip bclip usbip tmr3ip ?288 pir2 oscfif c1if c2if eeif bclif usbif tmr3if ?288 pie2 oscfie c1ie c2ie eeie bclie usbie tmr3ie ?288 legend: ? = unimplemented, read as ? 0 ?. shaded cells are not used during flash/eeprom access.
pic18f1xk50/pic18lf1xk50 ds41350e-page 60 preliminary ? 2010 microchip technology inc. notes:
? 2010 microchip technology inc. preliminary ds41350e-page 61 pic18f/lf1xk50 5.0 data eeprom memory the data eeprom is a nonvolatile memory array, separate from the data ram and program memory, which is used for long-term storage of program data. it is not directly mapped in either the register file or program memory space but is indirectly addressed through the special function registers (sfrs). the eeprom is readable and writable during normal operation over the entire v dd range. four sfrs are used to read and write to the data eeprom as well as the program memory. they are: ? eecon1 ? eecon2 ? eedata ? eeadr the data eeprom allows byte read and write. when interfacing to the data memory block, eedata holds the 8-bit data for read/write and the eeadr register pair hold the address of the eeprom location being accessed. the eeprom data memory is rated for high erase/write cycle endurance. a byte write automatically erases the location and writes the new data (erase-before-write). the write time is controlled by an on-chip timer; it will vary with voltage and temperature as well as from chip- to-chip. please refer to parameter us122 ( table 27-13 in section 27.0 ?electrical specifications? ) for exact limits. 5.1 eeadr register the eeadr register is used to address the data eeprom for read and write operations. the 8-bit range of the register can address a memory range of 256 bytes (00h to ffh). 5.2 eecon1 and eecon2 registers access to the data eeprom is controlled by two registers: eecon1 and eecon2. these are the same registers which control access to the program memory and are used in a similar manner for the data eeprom. the eecon1 register ( register 5-1 ) is the control register for data and program memory access. control bit eepgd determines if the access will be to program or data eeprom memory. when the eepgd bit is clear, operations will access the data eeprom memory. when the eepgd bit is set, program memory is accessed. control bit, cfgs, determines if the access will be to the configuration registers or to program memory/data eeprom memory. when the cfgs bit is set, subsequent operations access configuration registers. when the cfgs bit is clear, the eepgd bit selects either program flash or data eeprom memory. the wren bit, when set, will allow a write operation. on power-up, the wren bit is clear. the wrerr bit is set by hardware when the wr bit is set and cleared when the internal programming timer expires and the write operation is complete. the wr control bit initiates write operations. the bit can be set but not cleared by software. it is cleared only by hardware at the completion of the write operation. control bits, rd and wr, start read and erase/write operations, respectively. these bits are set by firmware and cleared by hardware at the completion of the operation. the rd bit cannot be set when accessing program memory (eepgd = 1 ). program memory is read using table read instructions. see section 4.1 ?table reads and table writes? regarding table reads. the eecon2 register is not a physical register. it is used exclusively in the memory write and erase sequences. reading eecon2 will read all ? 0 ?s. note: during normal operation, the wrerr may read as ? 1 ?. this can indicate that a write operation was prematurely termi- nated by a reset, or a write operation was attempted improperly. note: the eeif interrupt flag bit of the pir2 register is set when the write is complete. it must be cleared by software.
pic18f/lf1xk50 ds41350e-page 62 preliminary ? 2010 microchip technology inc. register 5-1: eecon1: data eeprom control 1 register r/w-x r/w-x u-0 r/w-0 r/w-x r/w-0 r/s-0 r/s-0 eepgd cfgs ? free wrerr wren wr rd bit 7 bit 0 legend: r = readable bit w = writable bit s = bit can be set by software, but not cleared u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 eepgd: flash program or data eeprom memory select bit 1 = access flash program memory 0 = access data eeprom memory bit 6 cfgs: flash program/data eeprom or configuration select bit 1 = access configuration registers 0 = access flash program or data eeprom memory bit 5 unimplemented: read as ? 0 ? bit 4 free: flash row (block) erase enable bit 1 = erase the program memory block addressed by tblptr on the next wr command (cleared by completion of erase operation) 0 = perform write-only bit 3 wrerr: flash program/data eeprom error flag bit (1) 1 = a write operation is prematurely terminated (any reset during self-timed programming in normal operation, or an improper write attempt) 0 = the write operation completed bit 2 wren: flash program/data eeprom write enable bit 1 = allows write cycles to flash program/data eeprom 0 = inhibits write cycles to flash program/data eeprom bit 1 wr: write control bit 1 = initiates a data eeprom erase/write cycle or a program memory erase cycle or write cycle. (the operation is self-timed and the bit is cleared by hardware once write is complete. the wr bit can only be set (not cleared) by software.) 0 = write cycle to the eeprom is complete bit 0 rd: read control bit 1 = initiates an eeprom read (read takes one cycle. rd is cleared by hardware. the rd bit can only be set (not cleared) by software. rd bit cannot be set when eepgd = 1 or cfgs = 1 .) 0 = does not initiate an eeprom read note 1: when a wrerr occurs, the eepgd and cfgs bits are not cleared. this allows tracing of the error condition.
? 2010 microchip technology inc. preliminary ds41350e-page 63 pic18f/lf1xk50 5.3 reading the data eeprom memory to read a data memory location, the user must write the address to the eeadr register, clear the eepgd con- trol bit of the eecon1 register and then set control bit, rd. the data is available on the very next instruction cycle; therefore, the eedata register can be read by the next instruction. eedata will hold this value until another read operation, or until it is written to by the user (during a write operation). the basic process is shown in example 5-1 . 5.4 writing to the data eeprom memory to write an eeprom data location, the address must first be written to the eeadr register and the data writ- ten to the eedata register. the sequence in example 5-2 must be followed to initiate the write cycle. the write will not begin if this sequence is not exactly followed (write 55h to eecon2, write 0aah to eecon2, then set wr bit) for each byte. it is strongly recommended that interrupts be disabled during this code segment. additionally, the wren bit in eecon1 must be set to enable writes. this mechanism prevents accidental writes to data eeprom due to unexpected code execution (i.e., runaway programs). the wren bit should be kept clear at all times, except when updating the eeprom. the wren bit is not cleared by hardware. after a write sequence has been initiated, eecon1, eeadr and eedata cannot be modified. the wr bit will be inhibited from being set unless the wren bit is set. both wr and wren cannot be set with the same instruction. at the completion of the write cycle, the wr bit is cleared by hardware and the eeprom interrupt flag bit, eeif, is set. the user may either enable this interrupt or poll this bit. eeif must be cleared by software. 5.5 write verify depending on the application, good programming practice may dictate that the value written to the memory should be verified against the original value. this should be used in applications where excessive writes can stress bits near the specification limit. example 5-1: data eeprom read example 5-2: data eeprom write movlw data_ee_addr ; movwf eeadr ; data memory address to read bcf eecon1, eepgd ; point to data memory bcf eecon1, cfgs ; access eeprom bsf eecon1, rd ; eeprom read movf eedata, w ; w = eedata movlw data_ee_addr_low ; movwf eeadr ; data memory address to write movlw data_ee_data ; movwf eedata ; data memory value to write bcf eecon1, eepgd ; point to data memory bcf eecon1, cfgs ; access eeprom bsf eecon1, wren ; enable writes bcf intcon, gie ; disable interrupts movlw 55h ; required movwf eecon2 ; write 55h sequence movlw 0aah ; movwf eecon2 ; write 0aah bsf eecon1, wr ; set wr bit to begin write bsf intcon, gie ; enable interrupts ; user code execution bcf eecon1, wren ; disable writes on write complete (eeif set)
pic18f/lf1xk50 ds41350e-page 64 preliminary ? 2010 microchip technology inc. 5.6 operation during code-protect data eeprom memory has its own code-protect bits in configuration words. external read and write operations are disabled if code protection is enabled. the microcontroller itself can both read and write to the internal data eeprom, regardless of the state of the code-protect configuration bit. refer to section 24.0 ?special features of the cpu? for additional information. 5.7 protection against spurious write there are conditions when the user may not want to write to the data eeprom memory. to protect against spurious eeprom writes, various mechanisms have been implemented. on power-up, the wren bit is cleared. in addition, writes to the eeprom are blocked during the power-up timer period (t pwrt , parameter 33). the write initiate sequence and the wren bit together help prevent an accidental write during brown-out, power glitch or software malfunction. 5.8 using the data eeprom the data eeprom is a high-endurance, byte addressable array that has been optimized for the storage of frequently changing information (e.g., program variables or other data that are updated often). when variables in one section change frequently, while variables in another section do not change, it is possible to exceed the total number of write cycles to the eeprom without exceeding the total number of write cycles to a single byte. if this is the case, then an array refresh must be performed. for this reason, variables that change infrequently (such as constants, ids, calibration, etc.) should be stored in flash program memory. example 5-3: data eeprom refresh routine table 5-1: registers associated with data eeprom memory name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 285 eeadr eeadr7 eeadr6 eeadr5 eeadr4 eeadr3 eeadr2 eeadr1 eeadr0 287 eedata eeprom data register 287 eecon2 eeprom control register 2 (not a physical register) 287 eecon1 eepgd cfgs ? free wrerr wren wr rd 287 ipr2 oscfip c1ip c2ip eeip bclip usbip tmr3ip ?288 pir2 oscfif c1if c2if eeif bclif usbif tmr3if ?288 pie2 oscfie c1ie c2ie eeie bclie usbie tmr3ie ?288 legend: ? = unimplemented, read as ? 0 ?. shaded cells are not used during flash/eeprom access. clrf eeadr ; start at address 0 bcf eecon1, cfgs ; set for memory bcf eecon1, eepgd ; set for data eeprom bcf intcon, gie ; disable interrupts bsf eecon1, wren ; enable writes loop ; loop to refresh array bsf eecon1, rd ; read current address movlw 55h ; movwf eecon2 ; write 55h movlw 0aah ; movwf eecon2 ; write 0aah bsf eecon1, wr ; set wr bit to begin write btfsc eecon1, wr ; wait for write to complete bra $-2 incfsz eeadr, f ; increment address bra loop ; not zero, do it again bcf eecon1, wren ; disable writes bsf intcon, gie ; enable interrupts
? 2010 microchip technology inc. preliminary ds41350e-page 65 pic18f/lf1xk50 6.0 8 x 8 hardware multiplier 6.1 introduction all pic18 devices include an 8 x 8 hardware multiplier as part of the alu. the multiplier performs an unsigned operation and yields a 16-bit result that is stored in the product register pair, prodh:prodl. the multiplier?s operation does not affect any flags in the status register. making multiplication a hardware operation allows it to be completed in a single instruction cycle. this has the advantages of higher computational throughput and reduced code size for multiplication algorithms and allows the pic18 devices to be used in many applica- tions previously reserved for digital signal processors. a comparison of various hardware and software multiply operations, along with the savings in memory and execution time, is shown in tab l e 6 - 1 . 6.2 operation example 6-1 shows the instruction sequence for an 8 x 8 unsigned multiplication. only one instruction is required when one of the arguments is already loaded in the wreg register. example 6-2 shows the sequence to do an 8 x 8 signed multiplication. to account for the sign bits of the argu- ments, each argument?s most significant bit (msb) is tested and the appropriate subtractions are done. example 6-1: 8 x 8 unsigned multiply routine example 6-2: 8 x 8 signed multiply routine table 6-1: performance comparison for v arious multiply operations movf arg1, w ; mulwf arg2 ; arg1 * arg2 -> ; prodh:prodl movf arg1, w mulwf arg2 ; arg1 * arg2 -> ; prodh:prodl btfsc arg2, sb ; test sign bit subwf prodh, f ; prodh = prodh ; - arg1 movf arg2, w btfsc arg1, sb ; test sign bit subwf prodh, f ; prodh = prodh ; - arg2 routine multiply method program memory (words) cycles (max) time @ 40 mhz @ 10 mhz @ 4 mhz 8 x 8 unsigned without hardware multiply 13 69 6.9 ? s27.6 ? s69 ? s hardware multiply 1 1 100 ns 400 ns 1 ? s 8 x 8 signed without hardware multiply 33 91 9.1 ? s36.4 ? s91 ? s hardware multiply 6 6 600 ns 2.4 ? s6 ? s 16 x 16 unsigned without hardware multiply 21 242 24.2 ? s96.8 ? s242 ? s hardware multiply 28 28 2.8 ? s 11.2 ? s28 ? s 16 x 16 signed without hardware multiply 52 254 25.4 ? s 102.6 ? s254 ? s hardware multiply 35 40 4.0 ? s16.0 ? s40 ? s
pic18f/lf1xk50 ds41350e-page 66 preliminary ? 2010 microchip technology inc. example 6-3 shows the sequence to do a 16 x 16 unsigned multiplication. equation 6-1 shows the algorithm that is used. the 32-bit result is stored in four registers (res<3:0>). equation 6-1: 16 x 16 unsigned multiplication algorithm example 6-3: 16 x 16 unsigned multiply routine example 6-4 shows the sequence to do a 16 x 16 signed multiply. equation 6-2 shows the algorithm used. the 32-bit result is stored in four registers (res<3:0>). to account for the sign bits of the argu- ments, the msb for each argument pair is tested and the appropriate subtractions are done. equation 6-2: 16 x 16 signed multiplication algorithm example 6-4: 16 x 16 signed multiply routine res3:res0 = arg1h:arg1l ? arg2h:arg2l = (arg1h ? arg2h ? 2 16 ) + (arg1h ? arg2l ? 2 8 ) + (arg1l ? arg2h ? 2 8 ) + (arg1l ? arg2l) movf arg1l, w mulwf arg2l ; arg1l * arg2l-> ; prodh:prodl movff prodh, res1 ; movff prodl, res0 ; ; movf arg1h, w mulwf arg2h ; arg1h * arg2h-> ; prodh:prodl movff prodh, res3 ; movff prodl, res2 ; ; movf arg1l, w mulwf arg2h ; arg1l * arg2h-> ; prodh:prodl movf prodl, w ; addwf res1, f ; add cross movf prodh, w ; products addwfc res2, f ; clrf wreg ; addwfc res3, f ; ; movf arg1h, w ; mulwf arg2l ; arg1h * arg2l-> ; prodh:prodl movf prodl, w ; addwf res1, f ; add cross movf prodh, w ; products addwfc res2, f ; clrf wreg ; addwfc res3, f ; res3:res0 = arg1h:arg1l ? arg2h:arg2l = (arg1h ? arg2h ? 2 16 ) + (arg1h ? arg2l ? 2 8 ) + (arg1l ? arg2h ? 2 8 ) + (arg1l ? arg2l) + (-1 ? arg2h<7> ? arg1h:arg1l ? 2 16 ) + (-1 ? arg1h<7> ? arg2h:arg2l ? 2 16 ) movf arg1l, w mulwf arg2l ; arg1l * arg2l -> ; prodh:prodl movff prodh, res1 ; movff prodl, res0 ; ; movf arg1h, w mulwf arg2h ; arg1h * arg2h -> ; prodh:prodl movff prodh, res3 ; movff prodl, res2 ; ; movf arg1l, w mulwf arg2h ; arg1l * arg2h -> ; prodh:prodl movf prodl, w ; addwf res1, f ; add cross movf prodh, w ; products addwfc res2, f ; clrf wreg ; addwfc res3, f ; ; movf arg1h, w ; mulwf arg2l ; arg1h * arg2l -> ; prodh:prodl movf prodl, w ; addwf res1, f ; add cross movf prodh, w ; products addwfc res2, f ; clrf wreg ; addwfc res3, f ; ; btfss arg2h, 7 ; arg2h:arg2l neg? bra sign_arg1 ; no, check arg1 movf arg1l, w ; subwf res2 ; movf arg1h, w ; subwfb res3 ; sign_arg1 btfss arg1h, 7 ; arg1h:arg1l neg? bra cont_code ; no, done movf arg2l, w ; subwf res2 ; movf arg2h, w ; subwfb res3 ; cont_code :
? 2010 microchip technology inc. preliminary ds41350e-page 67 pic18f/lf1xk50 7.0 interrupts the pic18f/lf1xk50 devices have multiple interrupt sources and an interrupt priority feature that allows most interrupt sources to be assigned a high priority level or a low priority level. the high priority interrupt vector is at 0008h and the low priority interrupt vector is at 0018h. a high priority interrupt event will interrupt a low priority interrupt that may be in progress. there are ten registers which are used to control interrupt operation. these registers are: ? rcon ?intcon ? intcon2 ? intcon3 ? pir1, pir2 ? pie1, pie2 ? ipr1, ipr2 it is recommended that the microchip header files sup- plied with mplab ? ide be used for the symbolic bit names in these registers. this allows the assembler/ compiler to automatically take care of the placement of these bits within the specified register. in general, interrupt sources have three bits to control their operation. they are: ? flag bit to indicate that an interrupt event occurred ? enable bit that allows program execution to branch to the interrupt vector address when the flag bit is set ? priority bit to select high priority or low priority 7.1 mid-range compatibility when the ipen bit is cleared (default state), the interrupt priority feature is disabled and interrupts are compatible with pic ? microcontroller mid-range devices. in compatibility mode, the interrupt priority bits of the iprx registers have no effect. the peie bit of the intcon register is the global interrupt enable for the peripherals. the peie bit disables only the peripheral interrupt sources and enables the peripheral interrupt sources when the gie bit is also set. the gie bit of the intcon register is the global interrupt enable which enables all non-peripheral interrupt sources and disables all interrupt sources, including the peripherals. all interrupts branch to address 0008h in compatibility mode. 7.2 interrupt priority the interrupt priority feature is enabled by setting the ipen bit of the rcon register. when interrupt priority is enabled the gie and peie global interrupt enable bits of compatibility mode are replaced by the gieh high priority, and giel low priority, global interrupt enables. when set, the gieh bit of the intcon regis- ter enables all interrupts that have their associated iprx register or intconx register priority bit set (high priority). when clear, the giel bit disables all interrupt sources including those selected as low priority. when clear, the giel bit of the intcon register disables only the interrupts that have their associated priority bit cleared (low priority). when set, the giel bit enables the low priority sources when the gieh bit is also set. when the interrupt flag, enable bit and appropriate global interrupt enable bit are all set, the interrupt will vector immediately to address 0008h for high priority, or 0018h for low priority, depending on level of the interrupting source?s priority bit. individual interrupts can be disabled through their corresponding interrupt enable bits. 7.3 interrupt response when an interrupt is responded to, the global interrupt enable bit is cleared to disable further interrupts. the gie bit is the global interrupt enable when the ipen bit is cleared. when the ipen bit is set, enabling interrupt priority levels, the gieh bit is the high priority global interrupt enable and the giel bit is the low priority global interrupt enable. high priority interrupt sources can interrupt a low priority interrupt. low priority interrupts are not processed while high priority interrupts are in progress. the return address is pushed onto the stack and the pc is loaded with the interrupt vector address (0008h or 0018h). once in the interrupt service routine, the source(s) of the interrupt can be determined by polling the interrupt flag bits in the intconx and pirx registers. the interrupt flag bits must be cleared by software before re-enabling interrupts to avoid repeating the same interrupt. the ?return from interrupt? instruction, retfie , exits the interrupt routine and sets the gie bit (gieh or giel if priority levels are used), which re-enables interrupts. for external interrupt events, such as the int pins or the portb interrupt-on-change, the interrupt latency will be three to four instruction cycles. the exact latency is the same for one-cycle or two-cycle
pic18f/lf1xk50 ds41350e-page 68 preliminary ? 2010 microchip technology inc. instructions. individual interrupt flag bits are set, regardless of the status of their corresponding enable bits or the global interrupt enable bit. note: do not use the movff instruction to mod- ify any of the interrupt control registers while any interrupt is enabled. doing so may cause erratic microcontroller behav- ior.
? 2010 microchip technology inc. preliminary ds41350e-page 69 pic18f/lf1xk50 figure 7-1: pic18 interrupt logic tmr0ie gieh/gie giel/peie wake-up if in interrupt to cpu vector to location 0008h int2if int2ie int2ip int1if int1ie int1ip tmr0if tmr0ie tmr0ip rabif rabie rabip ipen tmr0if tmr0ip int1if int1ie int1ip int2if int2ie int2ip rabif rabie rabip int0if int0ie giel/peie interrupt to cpu vector to location ipen ipen 0018h sspif sspie sspip sspif sspie sspip adif adie adip rcif rcie rcip additional peripheral interrupts adif adie adip high priority interrupt generation low priority interrupt generation rcif rcie rcip additional peripheral interrupts idle or sleep modes gieh/gie note 1: the rabif interrupt also requires the individual pin ioca and iocb enable. (1) (1)
pic18f/lf1xk50 ds41350e-page 70 preliminary ? 2010 microchip technology inc. 7.4 intcon registers the intcon registers are readable and writable registers, which contain various enable, priority and flag bits. note: interrupt flag bits are set when an interrupt condition occurs, regardless of the state of its corresponding enable bit or the global enable bit. user software should ensure the appropriate interrupt flag bits are clear prior to enabling an interrupt. this feature allows for software polling. register 7-1: intcon: interrupt control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-x gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 gie/gieh: global interrupt enable bit when ipen = 0 : 1 = enables all unmasked interrupts 0 = disables all interrupts including peripherals when ipen = 1 : 1 = enables all high priority interrupts 0 = disables all interrupts including low priority. bit 6 peie/giel: peripheral interrupt enable bit when ipen = 0 : 1 = enables all unmask ed peripheral interrupts 0 = disables all peripheral interrupts when ipen = 1 : 1 = enables all low priority interrupts 0 = disables all low priority interrupts bit 5 tmr0ie: tmr0 overflow interrupt enable bit 1 = enables the tmr0 overflow interrupt 0 = disables the tmr0 overflow interrupt bit 4 int0ie: int0 external interrupt enable bit 1 = enables the int0 external interrupt 0 = disables the int0 external interrupt bit 3 rabie: ra and rb port change interrupt enable bit (2) 1 = enables the ra and rb port change interrupt 0 = disables the ra and rb port change interrupt bit 2 tmr0if: tmr0 overflow interrupt flag bit 1 = tmr0 register has overflowed (must be cleared by software) 0 = tmr0 register did not overflow bit 1 int0if: int0 external interrupt flag bit 1 = the int0 external interrupt oc curred (must be cleared by software) 0 = the int0 external interrupt did not occur bit 0 rabif: ra and rb port change interrupt flag bit (1) 1 = at least one of the ra <5:3> or rb<7:4> pi ns changed state (must be cleared by software) 0 = none of the ra<5:3> or rb <7:4> pins have changed state note 1: a mismatch condition will continue to set the rabif bit. reading porta and portb will end the mismatch condition and allow the bit to be cleared. 2: ra and rb port change interrupts also requir e the individual pin ioca and iocb enable.
? 2010 microchip technology inc. preliminary ds41350e-page 71 pic18f/lf1xk50 register 7-2: intcon2: in terrupt control 2 register r/w-1 r/w-1 r/w-1 r/w-1 u-0 r/w-1 u-0 r/w-1 rabpu intedg0 intedg1 intedg2 ?tmr0ip ? rabip bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 rabpu : porta and portb pull-up enable bit 1 = all porta and portb pull-ups are disabled 0 = porta and portb pull-ups are enabled provided that the pin is an input and the corresponding wpua and wpub bits are set. bit 6 intedg0: external interrupt 0 edge select bit 1 = interrupt on rising edge 0 = interrupt on falling edge bit 5 intedg1: external interrupt 1 edge select bit 1 = interrupt on rising edge 0 = interrupt on falling edge bit 4 intedg2: external interrupt 2 edge select bit 1 = interrupt on rising edge 0 = interrupt on falling edge bit 3 unimplemented: read as ? 0 ? bit 2 tmr0ip: tmr0 overflow interrupt priority bit 1 =high priority 0 = low priority bit 1 unimplemented: read as ? 0 ? bit 0 rabip: ra and rb port change interrupt priority bit 1 =high priority 0 = low priority note: interrupt flag bits are set when an interrupt condition occurs, regardless of the state of its corresponding enable bit or the global enable bit. user software should ensure the appropriate interrupt flag bits are clear prior to enabling an interrupt. this feature allows for software polling.
pic18f/lf1xk50 ds41350e-page 72 preliminary ? 2010 microchip technology inc. register 7-3: intcon3: in terrupt control 3 register r/w-1 r/w-1 u-0 r/w-0 r/w-0 u-0 r/w-0 r/w-0 int2ip int1ip ? int2ie int1ie ? int2if int1if bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 int2ip: int2 external interrupt priority bit 1 = high priority 0 = low priority bit 6 int1ip: int1 external interrupt priority bit 1 = high priority 0 = low priority bit 5 unimplemented: read as ? 0 ? bit 4 int2ie: int2 external interrupt enable bit 1 = enables the int2 external interrupt 0 = disables the int2 external interrupt bit 3 int1ie: int1 external interrupt enable bit 1 = enables the int1 external interrupt 0 = disables the int1 external interrupt bit 2 unimplemented: read as ? 0 ? bit 1 int2if: int2 external interrupt flag bit 1 = the int2 external interrupt occurred (must be cleared by software) 0 = the int2 external interrupt did not occur bit 0 int1if: int1 external interrupt flag bit 1 = the int1 external interrupt occurred (must be cleared by software) 0 = the int1 external interrupt did not occur note: interrupt flag bits are set when an interrupt condition occurs, regardless of the state of its corresponding enable bit or the global enable bit. user software should ensure the appropriate interrupt flag bits are clear prior to enabling an interrupt. this feature allows for software polling.
? 2010 microchip technology inc. preliminary ds41350e-page 73 pic18f/lf1xk50 7.5 pir registers the pir registers contain the individual flag bits for the peripheral interrupts. due to the number of peripheral interrupt sources, there are two peripheral interrupt request flag registers (pir1 and pir2). note 1: interrupt flag bits are set when an inter- rupt condition occurs, regardless of the state of its corresponding enable bit or the global interrupt enable bit, gie of the intcon register. 2: user software should ensure the appro- priate interrupt flag bits are cleared prior to enabling an interrupt and after servicing that interrupt. register 7-4: pir1: peripheral interrupt request (flag) register 1 u-0 r/w-0 r-0 r-0 r/w-0 r/w-0 r/w-0 r/w-0 ? adif rcif txif sspif ccp1if tmr2if tmr1if bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 unimplemented: read as ? 0 ? bit 6 adif: a/d converter interrupt flag bit 1 = an a/d conversion completed (must be cleared by software) 0 = the a/d conversion is not complete or has not been started bit 5 rcif: eusart receive interrupt flag bit 1 = the eusart receive buffer, rcreg, is full (cleared when rcreg is read) 0 = the eusart receive buffer is empty bit 4 txif: eusart transmit interrupt flag bit 1 = the eusart transmit buffer, txreg, is empty (cleared when txreg is written) 0 = the eusart transmit buffer is full bit 3 sspif: master synchronous serial port interrupt flag bit 1 = the transmission/reception is complete (must be cleared by software) 0 = waiting to transmit/receive bit 2 ccp1if: ccp1 interrupt flag bit capture mode: 1 = a tmr1 register capture occurred (must be cleared by software) 0 = no tmr1 register capture occurred compare mode: 1 = a tmr1 register compare match occurred (must be cleared by software) 0 = no tmr1 register compare match occurred pwm mode: unused in this mode bit 1 tmr2if: tmr2 to pr2 match interrupt flag bit 1 = tmr2 to pr2 match occurred (must be cleared by software) 0 = no tmr2 to pr2 match occurred bit 0 tmr1if: tmr1 overflow interrupt flag bit 1 = tmr1 register overflowed (must be cleared by software) 0 = tmr1 register did not overflow
pic18f/lf1xk50 ds41350e-page 74 preliminary ? 2010 microchip technology inc. register 7-5: pir2: peripheral interrupt request (flag) register 2 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 u-0 oscfif c1if c2if eeif bclif usbif tmr3if ? bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 oscfif: oscillator fail interrupt flag bit 1 = device oscillator failed, clock input has changed to hfintosc (must be cleared by software) 0 = device clock operating bit 6 c1if: comparator c1 interrupt flag bit 1 = comparator c1 output has changed (must be cleared by software) 0 = comparator c1 output has not changed bit 5 c2if: comparator c2 interrupt flag bit 1 = comparator c2 output has changed (must be cleared by software) 0 = comparator c2 output has not changed bit 4 eeif: data eeprom/flash write operation interrupt flag bit 1 = the write operation is complete (must be cleared by software) 0 = the write operation is not complete or has not been started bit 3 bclif: bus collision interrupt flag bit 1 = a bus collision occurred (must be cleared by software) 0 = no bus collision occurred bit 2 usbif: usb interrupt flag bit 1 = usb has requested an interrupt (must be cleared in software) 0 = no usb interrupt request bit 1 tmr3if: tmr3 overflow interrupt flag bit 1 = tmr3 register overflowed (must be cleared by software) 0 = tmr3 register did not overflow bit 0 unimplemented: read as ? 0 ?
? 2010 microchip technology inc. preliminary ds41350e-page 75 pic18f/lf1xk50 7.6 pie registers the pie registers contain the individual enable bits for the peripheral interrupts. due to the number of periph- eral interrupt sources, there are two peripheral interrupt enable registers (pie1 and pie2). when ipen = 0 , the peie bit must be set to enable any of these peripheral interrupts. register 7-6: pie1: peripheral interrupt enable (flag) register 1 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ? adie rcie txie sspie ccp1ie tmr2ie tmr1ie bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 unimplemented: read as ? 0 ? bit 6 adie: a/d converter interrupt enable bit 1 = enables the a/d interrupt 0 = disables the a/d interrupt bit 5 rcie: eusart receive interrupt enable bit 1 = enables the eusart receive interrupt 0 = disables the eusart receive interrupt bit 4 txie: eusart transmit interrupt enable bit 1 = enables the eusart transmit interrupt 0 = disables the eusart transmit interrupt bit 3 sspie: master synchronous serial port interrupt enable bit 1 = enables the mssp interrupt 0 = disables the mssp interrupt bit 2 ccp1ie: ccp1 interrupt enable bit 1 = enables the ccp1 interrupt 0 = disables the ccp1 interrupt bit 1 tmr2ie: tmr2 to pr2 match interrupt enable bit 1 = enables the tmr2 to pr2 match interrupt 0 = disables the tmr2 to pr2 match interrupt bit 0 tmr1ie: tmr1 overflow interrupt enable bit 1 = enables the tmr1 overflow interrupt 0 = disables the tmr1 overflow interrupt
pic18f/lf1xk50 ds41350e-page 76 preliminary ? 2010 microchip technology inc. register 7-7: pie2: peripheral interrupt enable (flag) register 2 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 u-0 oscfie c1ie c2ie eeie bclie usbie tmr3ie ? bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 oscfie: oscillator fail interrupt enable bit 1 = enabled 0 =disabled bit 6 c1ie: comparator c1 interrupt enable bit 1 = enabled 0 = disabled bit 5 c2ie: comparator c2 interrupt enable bit 1 = enabled 0 = disabled bit 4 eeie: data eeprom/flash write operation interrupt enable bit 1 = enabled 0 =disabled bit 3 bclie: bus collision interrupt enable bit 1 = enabled 0 =disabled bit 2 usbie: usb interrupt enable bit 1 = enabled 0 = disabled bit 1 tmr3ie: tmr3 overflow interrupt enable bit 1 = enabled 0 =disabled bit 0 unimplemented: read as ? 0 ?
? 2010 microchip technology inc. preliminary ds41350e-page 77 pic18f/lf1xk50 7.7 ipr registers the ipr registers contain the individual priority bits for the peripheral interrupts. due to the number of peripheral interrupt sources, there are two peripheral interrupt priority registers (ipr1 and ipr2). using the priority bits requires that the interrupt priority enable (ipen) bit be set. register 7-8: ipr1: peripheral interrupt priority register 1 u-0 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 ? adip rcip txip sspip ccp1ip tmr2ip tmr1ip bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 unimplemented: read as ? 0 ? bit 6 adip: a/d converter interrupt priority bit 1 = high priority 0 = low priority bit 5 rcip: eusart receive interrupt priority bit 1 = high priority 0 = low priority bit 4 txip: eusart transmit interrupt priority bit 1 = high priority 0 = low priority bit 3 sspip: master synchronous serial port interrupt priority bit 1 = high priority 0 = low priority bit 2 ccp1ip: ccp1 interrupt priority bit 1 = high priority 0 = low priority bit 1 tmr2ip: tmr2 to pr2 match interrupt priority bit 1 = high priority 0 = low priority bit 0 tmr1ip: tmr1 overflow interrupt priority bit 1 = high priority 0 = low priority
pic18f/lf1xk50 ds41350e-page 78 preliminary ? 2010 microchip technology inc. register 7-9: ipr2: peripheral interrupt priority register 2 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 u-0 oscfip c1ip c2ip eeip bclip usbip tmr3ip ? bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 oscfip: oscillator fail interrupt priority bit 1 = high priority 0 = low priority bit 6 c1ip: comparator c1 interrupt priority bit 1 = high priority 0 = low priority bit 5 c2ip: comparator c2 interrupt priority bit 1 = high priority 0 = low priority bit 4 eeip: data eeprom/flash write operation interrupt priority bit 1 = high priority 0 = low priority bit 3 bclip: bus collision interrupt priority bit 1 = high priority 0 = low priority bit 2 usbip: usb interrupt priority bit 1 = high priority 0 = low priority bit 1 tmr3ip: tmr3 overflow interrupt priority bit 1 = high priority 0 = low priority bit 0 unimplemented: read as ? 0 ?
? 2010 microchip technology inc. preliminary ds41350e-page 79 pic18f/lf1xk50 7.8 rcon register the rcon register contains flag bits which are used to determine the cause of the last reset or wake-up from idle or sleep modes. rcon also contains the ipen bit which enables interrupt priorities. the operation of the sboren bit and the reset flag bits is discussed in more detail in section 23.1 ?rcon register? . register 7-10: rcon: re set control register r/w-0 r/w-1 u-0 r/w-1 r-1 r-1 r/w-0 r/w-0 ipen sboren (1) ?ri to pd por (2) bor bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 ipen: interrupt priority enable bit 1 = enable priority levels on interrupts 0 = disable priority levels on interrupts (pic16cxxx compatibility mode) bit 6 sboren: bor software enable bit (1) if boren<1:0> = 01 : 1 = bor is enabled 0 = bor is disabled if boren<1:0> = 00 , 10 or 11 : bit is disabled and read as ? 0 ?. bit 5 unimplemented: read as ? 0 ? bit 4 ri : reset instruction flag bit 1 = the reset instruction was not executed (set by firmware or power-on reset) 0 = the reset instruction was executed causing a device reset (must be set in firmware after a code-executed reset occurs) bit 3 to : watchdog time-out flag bit 1 = set by power-up, clrwdt instruction or sleep instruction 0 = a wdt time-out occurred bit 2 pd : power-down detection flag bit 1 = set by power-up or by the clrwdt instruction 0 = set by execution of the sleep instruction bit 1 por : power-on reset status bit (2) 1 = no power-on reset occurred 0 = a power-on reset occurred (must be set in software after a power-on reset occurs) bit 0 bor : brown-out reset status bit (3) 1 = a brown-out reset has not occurred (set by firmware only) 0 = a brown-out reset occurred (must be set by firmware after a por or brown-out reset occurs) note 1: if sboren is enabled, its reset state is ? 1 ?; otherwise, it is ? 0 ?. 2: the actual reset value of por is determined by the type of device reset. see the notes following this register and section 23.6 ?reset state of registers? for additional information. 3: see table 23-3 .
pic18f/lf1xk50 ds41350e-page 80 preliminary ? 2010 microchip technology inc. 7.9 intn pin interrupts external interrupts on the rc0/int0, rc1/int1 and rc2/int2 pins are edge-triggered. if the corresponding intedgx bit in the intcon2 register is set (= 1 ), the interrupt is triggered by a rising edge; if the bit is clear, the trigger is on the falling edge. when a valid edge appears on the rcx/intx pin, the corresponding flag bit, intxf, is set. this interrupt can be disabled by clearing the corresponding enable bit, intxe. flag bit, intxf, must be cleared by software in the interrupt service routine before re-enabling the interrupt. all external interrupts (int0, int1 and int2) can wake- up the processor from idle or sleep modes if bit intxe was set prior to going into those modes. if the global interrupt enable bit, gie, is set, the processor will branch to the interrupt vector following wake-up. interrupt priority for int1 and int2 is determined by the value contained in the interrupt priority bits, int1ip and int2ip of the intcon3 register. there is no priority bit associated with int0. it is always a high priority interrupt source. 7.10 tmr0 interrupt in 8-bit mode (which is the default), an overflow in the tmr0 register (ffh ? 00h) will set flag bit, tmr0if. in 16-bit mode, an overflow in the tmr0h:tmr0l regis- ter pair (ffffh ?? 0000h) will set tmr0if. the interrupt can be enabled/disabled by setting/clearing enable bit, tmr0ie of the intcon register. interrupt priority for timer0 is determined by the value contained in the interrupt priority bit, tmr0ip of the intcon2 register. see section 10.0 ?timer0 module? for further details on the timer0 module. 7.11 porta and portb interrupt-on- change an input change on porta or portb sets flag bit, rabif of the intcon register. the interrupt can be enabled/disabled by setting/clearing enable bit, rabie of the intcon register. pins must also be individually enabled with the ioca and iocb register. interrupt priority for porta and portb interrupt-on-change is determined by the value contained in the interrupt priority bit, rabip of the intcon2 register. 7.12 context saving during interrupts during interrupts, the return pc address is saved on the stack. additionally, the wreg, status and bsr registers are saved on the fast return stack. if a fast return from interrupt is not used (see section 3.3 ?data memory organization? ), the user may need to save the wreg, status and bsr registers on entry to the interrupt service routine. depending on the user?s application, other registers may also need to be saved. example 7-1 saves and restores the wreg, status and bsr registers during an interrupt service routine. example 7-1: saving status, wr eg and bsr registers in ram movwf w_temp ; w_temp is in virtual bank movff status, status_temp ; status_temp located anywhere movff bsr, bsr_temp ; bsr_tmep located anywhere ; ; user isr code ; movff bsr_temp, bsr ; restore bsr movf w_temp, w ; restore wreg movff status_temp, status ; restore status
? 2010 microchip technology inc. preliminary ds41350e-page 81 pic18f1xk50/pic18lf1xk50 8.0 low dropout (ldo) voltage regulator the pic18f1xk50 devices differ from the pic18lf1xk50 devices due to an internal low dropout (ldo) voltage regulator. the pic18f1xk50 contain an internal ldo, while the pic18lf1xk50 do not. the lithography of the die allows a maximum operating voltage of the nominal 3.6v on the internal digital logic. in order to continue to support 5.0v designs, a ldo voltage regulator is integrated on the die. the ldo voltage regulator allows for the internal digital logic to operate at 3.3v, while i/o?s operate at 5.0v (v dd ). the ldo voltage regulator requires an external bypass capacitor for stability. the v usb pin is required to have an external bypass capacitor. it is recommended that the capacitor be a ceramic cap between 0.22 to 0.47 f. on power-up, the external capacitor will look like a large load on the ldo voltage regulator. to prevent erroneous operation, the device is held in reset while a constant current source charges the external capacitor. after the cap is fully charged, the device is released from reset. for more information, refer to section 27.0 ?electrical specifications? .
pic18f1xk50/pic18lf1xk50 ds41350e-page 82 preliminary ? 2010 microchip technology inc. notes:
? 2010 microchip technology inc. preliminary ds41350e-page 83 pic18f/lf1xk50 9.0 i/o ports there are up to three ports available. some pins of the i/o ports are multiplexed with an alternate function from the peripheral features on the device. in general, when a peripheral is enabled, that pin may not be used as a general purpose i/o pin. each port has three registers for its operation. these registers are: ? tris register (data direction register) ? port register (reads the levels on the pins of the device) ? lat register (output latch) the porta data latch (lata register) is useful for read-modify-write operations on the value that the i/o pins are driving. a simplified model of a generic i/o port, without the interfaces to other peripherals, is shown in figure 9-1 . figure 9-1: generic i/o port operation 9.1 porta, trisa and lata registers porta is 5 bits wide. porta<5:4> bits are bidirectional ports and porta<3,1:0> bits are input- only ports. the corresponding data direction register is trisa. setting a trisa bit (= 1 ) will make the corresponding porta pin an input (i.e., disable the output driver). clearing a trisa bit (= 0 ) will make the corresponding porta pin an output (i.e., enable the output driver and put the contents of the output latch on the selected pin). reading the porta register reads the status of the pins, whereas writing to it, will write to the port latch. the porta data latch (lata) register is also memory mapped. read-modify-write operations on the lata register read and write the latched output value for porta. all of the porta pins are individually configurable as interrupt-on-change pins. control bits in the ioca register enable (when set) or disable (when clear) the interrupt function for each pin. when set, the rabie bit of the intcon register enables interrupts on all pins which also have their corresponding ioca bit set. when clear, the rabie bit disables all interrupt-on-changes. only pins configured as inputs can cause this interrupt to occur (i.e., any pin configured as an output is excluded from the interrupt-on-change comparison). for enabled interrupt-on-change pins, the values are compared with the old value latched on the last read of porta. the ?mismatch? outputs of the last read are or?d together to set the porta change interrupt flag bit (rabif) in the intcon register. this interrupt can wake the device from the sleep mode, or any of the idle modes. the user, in the interrupt service routine, can clear the interrupt in the following manner: a) any read or write of porta to clear the mis- match condition (except when porta is the source or destination of a movff instruction). b) clear the flag bit, rabif. a mismatch condition will continue to set the rabif flag bit. reading or writing porta will end the mismatch condition and allow the rabif bit to be cleared. the latch holding the last read value is not affected by a mclr nor brown-out reset. after either one of these resets, the rabif flag will continue to be set if a mismatch is present. data bus wr lat wr tris rd port data latch tris latch rd tris input buffer i/o pin (1) q d ck q d ck en qd en rd lat or port note 1: i/o pins have diode protection to v dd and v ss .
pic18f/lf1xk50 ds41350e-page 84 preliminary ? 2010 microchip technology inc. the interrupt-on-change feature is recommended for wake-up on key depression operation and operations where porta is only used for the interrupt-on-change feature. polling of porta is not recommended while using the interrupt-on-change feature. each of the porta pins has an individually controlled weak internal pull-up. when set, each bit of the wpua register enables the corresponding pin pull-up. when cleared, the rabpu bit of the intcon2 register enables pull-ups on all pins which also have their cor- responding wpua bit set. when set, the rabpu bit disables all weak pull-ups. the weak pull-up is auto- matically turned off when the port pin is configured as an output. the pull-ups are disabled on a power-on reset. ra0 and ra1 are multiplexed with the usb module and can serve as the differential data lines for the on- chip usb transceiver. ra0 and ra1 do not have trisa bits associated with them. as digital port pins, they can only function as digital inputs. when configured for usb operation, the data direction is determined by the configuration and status of the usb module at a given time. ra3 is an input only pin. its operation is controlled by the mclre bit of the config3h register. when selected as a port pin (mclre = 0 ), it functions as a digital input only pin; as such, it does not have tris or lat bits associated with its operation. pins ra4 and ra5 are multiplexed with the main oscil- lator pins; they are enabled as oscillator or i/o pins by the selection of the main oscillator in the configuration register (see section 24.1 ?configuration bits? for details). when they are not used as port pins, ra4 and ra5 and their associated tris and lat bits read as ? 0 ?. pin ra4 is multiplexed with an analog input. the oper- ation of pin ra4 as analog is selected by setting the ans3 bit in the ansel register which is the default set- ting after a power-on reset. example 9-1: initiali zing porta note 1: if a change on the i/o pin should occur when the read operation is being exe- cuted (start of the q2 cycle), then the rabif interrupt flag may not get set. fur- thermore, since a read or write on a port affects all bits of that port, care must be taken when using multiple pins in inter- rupt-on-change mode. changes on one pin may not be seen while servicing changes on another pin. 2: when configured for usb operation, interrupt-on-change functionality on ra0 and ra1 is automatically disabled. 3: in order for the digital inputs to function on the ra<1:0> port pins, the interrupt- on-change pins must be enabled (ioca <1:0> = 11 ) and the usb module must be disabled (usben = 0 ). note: on a power-on reset, ra4 is configured as analog inputs by default and read as ? 0 ?; ra<1:0> and ra<5:3> are configured as digital inputs. note: on a power-on reset, ra3 is enabled as a digital input only if master clear functionality is disabled. note: on a power-on reset, ra4 is configured as analog inputs and read as ? 0 ?. clrf porta ; initialize porta by ; clearing output ; data latches clrf lata ; alternate method ; to clear output ; data latches movlw 030h ; value used to ; initialize data ; direction movwf trisa ; set ra<5:4> as output
? 2010 microchip technology inc. preliminary ds41350e-page 85 pic18f/lf1xk50 register 9-1: porta: porta register u-0 u-0 r/w-x r/w-x r-x u-0 r/w-x r/w-x ? ?ra5ra4ra3 ?ra1ra0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-6 unimplemented : read as ? 0 ? bit 5-3 ra<5:3> : porta i/o pin bit (1) 1 = port pin is > v ih 0 = port pin is < v il bit 2 unimplemented : read as ? 0 ? bit 1-0 ra<1:0> : porta i/o pin bit 1 = port pin is > v ih 0 = port pin is < v il note 1: the ra3 bit is only available when master clear reset is disabled (mclre configuration bit = 0 ). otherwise, ra3 reads as ? 0 ?. this bit is read-only. register 9-2: trisa: porta tri-state register u-0 u-0 r/w-1 r/w-1 u-0 u-0 u-0 u-0 ? ? trisa5 trisa4 ? ? ? ? bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-6 unimplemented : read as ? 0 ? bit 5-4 trisa<5:4>: porta tri-state control bit 1 = porta pin configured as an input (tri-stated) 0 = porta pin configured as an output bit 3-0 unimplemented : read as ? 0 ? note 1: trisa<5:4> always reads ? 1 ? in xt, hs and lp oscillator modes.
pic18f/lf1xk50 ds41350e-page 86 preliminary ? 2010 microchip technology inc. register 9-3: wpua: weak pull-up porta register u-0 u-0 r/w-1 r/w-1 rw-1 u-0 u-0 u-0 ? ? wpua5 wpua4 wpua3 ? ? ? bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-6 unimplemented : read as ? 0 ? bit 5-3 wpua<5:3>: weak pull-up enable bit 1 = pull-up enabled 0 = pull-up disabled bit 2 unimplemented : read as ? 0 ? bit 1-0 wpua<1:0>: weak pull-up enable bit 1 = pull-up enabled 0 = pull-up disabled register 9-4: ioca: interrupt-on-change porta register u-0 u-0 r/w-0 r/w-0 r-0 u-0 r/w-0 r/w-0 ? ? ioca5 ioca4 ioca3 ? ioca1 ioca0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-6 unimplemented : read as ? 0 ? bit 5-3 ioca<5:3> : porta i/o pin bit 1 = interrupt-on-change enabled 0 = interrupt-on-change disabled bit 2 unimplemented : read as ? 0 ? bit 1-0 ioca<1:0> : porta i/o pin bit 1 = interrupt-on-change enabled 0 = interrupt-on-change disabled register 9-5: lata: porta data latch register u-0 u-0 r/w-x r/w-x u-0 u-0 u-0 u-0 ? ?lata5lata4 ? ? ? ? bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-6 unimplemented : read as ? 0 ? bit 5-4 lata<5:4> : ra<5:4> port i/o output latch register bits bit 3-0 unimplemented : read as ? 0 ?
? 2010 microchip technology inc. preliminary ds41350e-page 87 pic18f/lf1xk50 table 9-1: porta i/o summary pin function tris setting i/o i/o type description ra0/ioca0/d+/ pgd ra0 ? (1) i ttl porta<0> data input; disabled when usb enabled. ioca0 ? (1) i ttl interrupt-on-pin change; disabled when usb enabled. d+ ? (1) i xcvr usb bus differential plus line input (internal transceiver). ? (1) o xcvr usb bus differential plus line output (internal transceiver). pgd ? (1) o dig serial execution data output for icsp?. ? (1) i st serial execution data input for icsp?. ra1/ioca1/d-/ pgc ra1 ? (1) i ttl porta<1> data input; disabled when usb enabled. ioca1 ? (1) i ttl interrupt-on-pin change; disabled when usb enabled. d- ? (1) i xcvr usb bus differential minus line input (internal transceiver). ? (1) o xcvr usb bus differential minus line output (internal transceiver). pgc ? (1) o dig serial execution clock output for icsp?. ? (1) i st serial execution clock input for icsp?. ra3/ioca3/mclr/ v pp ra3 ? (2) i st porta<3> data input; enabled when mclre configuration bit is clear; programmable weak pull-up. ioca3 ? (1) i ttl interrupt-on-pin change mclr ? i st external master clear input; enabled when mclre configuration bit is set. v pp ? i ana high-voltage detection; used for icsp? mode entry detection. always available, regardless of pin mode. ra4/ioca4/an3/ osc2/clkout ra4 0 o dig lata<4> data output. enabled in rcio, intio2 and ecio modes only. 1 i ttl porta<4> data input; programmable weak pull-up. enabled in rcio, intio2 and ecio modes only. ioca4 1 i ttl interrupt-on-pin change an3 1 i ana a/d input channel 3. default configuration on por. osc2 x o ana main oscillator feedback output connection (xt, hs and lp modes). clkout x o dig system cycle clock output (f osc /4) in rc, intio1 and ec oscillator modes. ra5/ioca5/osc1/ clkin ra5 0 o dig lata<5> data output. disabled in external oscillator modes. 1 i ttl porta<5> data input. disabled in external oscillator modes; program- mable weak pull-up. ioca5 1 i ttl interrupt-on-pin change osc1 x i ana main oscillator input connection. clkin x i ana main clock input connection. legend: dig = digital level output; ttl = ttl input buffer; st = schmitt trigger input buffer; ana = analog level input/output; x = don?t care (tris bit does not affect port direction or is overridden for this option). note 1: ra0 and ra1 do not have corresponding trisa bits. in port mode, these pins are input only. usb data direction is determined by the usb configuration. 2: ra3 does not have a corresponding trisa bit. this pin is always an input regardless of mode.
pic18f/lf1xk50 ds41350e-page 88 preliminary ? 2010 microchip technology inc. table 9-2: summary of registers associated with porta name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page porta ? ? ra5 (1) ra4 (1) ra3 (2) ? ra1 (3) ra0 (3) 288 lata ? ?lata5 (1) lata4 (1) ? ? ? ?288 trisa ? ? trisa5 (1) trisa4 (1) ? ? ? ?288 ansel ans7 ans6 ans5 ans4 ans3 ? ? ?288 slrcon ? ? ? ? ? slrc slrb slra 288 ioca ? ?ioca5ioca4 ioca3 (2) ? ioca1 (3) ioca0 (3) 288 wpua ? ?wpua5wpua4 wpua3 (2) ? ? ?288 ucon ? ppbrst se0 pktdis usben resume suspnd ?288 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 285 intcon2 rabpu intedg0 intedg1 intedg2 ? tmr0ip ? rabip 285 legend: ? = unimplemented, read as ? 0 ?. shaded cells are not used by porta. note 1: ra<5:4> and their associated latch and data direction bits are enabled as i/o pins based on oscillator configuration; otherwise, they are read as ? 0 ?. 2: implemented only when master clear functionality is disabled (mclre configuration bit = 0 ). 3: ra1 and ra0 are only available as port pins when the usb module is disabled (ucon<3> = 0 ).
? 2010 microchip technology inc. preliminary ds41350e-page 89 pic18f/lf1xk50 9.2 portb, trisb and latb registers portb is an 4-bit wide, bidirectional port. the corre- sponding data direction register is trisb. setting a trisb bit (= 1 ) will make the corresponding portb pin an input (i.e., disable the output driver). clearing a trisb bit (= 0 ) will make the corresponding portb pin an output (i.e., enable the output driver and put the contents of the output latch on the selected pin). the portb data latch register (latb) is also memory mapped. read-modify-write operations on the latb register read and write the latched output value for portb. example 9-2: initializing portb all portb pins are individually configurable as interrupt-on-change pins. control bits in the iocb reg- ister enable (when set) or disable (when clear) the interrupt function for each pin. when set, the rabie bit of the intcon register enables interrupts on all pins which also have their corresponding iocb bit set. when clear, the rabie bit disables all interrupt-on-changes. only pins configured as inputs can cause this interrupt to occur (i.e., any pin configured as an output is excluded from the interrupt-on-change comparison). for enabled interrupt-on-change pins, the values are compared with the old value latched on the last read of portb. the ?mismatch? outputs of the last read are or?d together to set the portb change interrupt flag bit (rabif) in the intcon register. this interrupt can wake the device from the sleep mode, or any of the idle modes. the user, in the interrupt service routine, can clear the interrupt in the following manner: a) any read or write of portb to clear the mis- match condition (except when portb is the source or destination of a movff instruction). b) clear the flag bit, rabif. a mismatch condition will continue to set the rabif flag bit. reading or writing portb will end the mismatch condition and allow the rabif bit to be cleared. the latch holding the last read value is not affected by a mclr nor brown-out reset. after either one of these resets, the rabif flag will continue to be set if a mismatch is present. the interrupt-on-change feature is recommended for wake-up on key depression operation and operations where portb is only used for the interrupt-on-change feature. polling of portb is not recommended while using the interrupt-on-change feature. all portb pins have individually controlled weak inter- nal pull-up. when set, each bit of the wpub register enables the corresponding pin pull-up. when cleared, the rabpu bit of the intcon2 register enables pull- ups on all pins which also have their corresponding wpub bit set. when set, the rabpu bit disables all weak pull-ups. the weak pull-up is automatically turned off when the port pin is configured as an output. the pull-ups are disabled on a power-on reset. clrf portb ; initialize portb by ; clearing output ; data latches clrf latb ; alternate method ; to clear output ; data latches movlw 0f0h ; value used to ; initialize data ; direction movwf trisb ; set rb<7:4> as outputs note: if a change on the i/o pin should occur when the read operation is being executed (start of the q2 cycle), then the rabif interrupt flag may not get set. furthermore, since a read or write on a port affects all bits of that port, care must be taken when using multiple pins in interrupt-on-change mode. changes on one pin may not be seen while servicing changes on another pin. note: on a power-on reset, rb<5:4> are configured as analog inputs by default and read as ? 0 ?.
pic18f/lf1xk50 ds41350e-page 90 preliminary ? 2010 microchip technology inc. register 9-6: port b: portb register r/w-x r/w-x r/w-x r/w-x u-0 u-0 u-0 u-0 rb7 rb6 rb5 rb4 ? ? ? ? bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-4 rb<7:4> : portb i/o pin bit 1 = port pin is >v ih 0 = port pin is : portb tri-state control bit 1 = portb pin configured as an input (tri-stated) 0 = portb pin configured as an output bit 3-0 unimplemented : read as ? 0 ?
? 2010 microchip technology inc. preliminary ds41350e-page 91 pic18f/lf1xk50 register 9-8: wpub: weak pull-up portb register r/w-1 r/w-1 r/w-1 r/w-1 u-0 u-0 u-0 u-0 wpub7 wpub6 wpub5 wpub4 ? ? ? ? bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-4 wpub<7:4>: weak pull-up enable bit 1 = pull-up enabled 0 = pull-up disabled bit 3-0 unimplemented : read as ? 0 ? register 9-9: iocb: interrup t-on-change portb register r/w-0 r/w-0 r/w-0 r/w-0 u-0 u-0 u-0 u-0 iocb7 iocb6 iocb5 iocb4 ? ? ? ? bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-4 iocb<7:4> : interrupt-on-change bits 1 = interrupt-on-change enabled 0 = interrupt-on-change disabled bit 3-0 unimplemented : read as ? 0 ? register 9-10: latb: portb data latch register r/w-x r/w-x r/w-x r/w-x u-0 u-0 u-0 u-0 latb7 latb6 latb5 latb4 ? ? ? ? bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-4 latb<7:4> : rb<7:4> port i/o output latch register bits bit 3-0 unimplemented : read as ? 0 ?
pic18f/lf1xk50 ds41350e-page 92 preliminary ? 2010 microchip technology inc. table 9-3: portb i/o summary pin function tris setting i/o i/o type description rb4/iocb4/an10/ sdi/sda rb4 0 o dig latb<4> data output; not affected by analog input. 1 i ttl portb<4> data input; programmable weak pull-up. iocb4 1 i ttl interrupt-on-pin change. an10 1 i ana adc input channel 10. sdi 1 i st spi data input (mssp module). sda 1 idigi 2 c? data output (mssp module); takes priority over port data. 1 oi2ci 2 c? data input (mssp module); input type depends on module setting. rb5/iocb5/an11/ rx/dt rb5 0 o dig latb<5> data output. 1 i ttl portb<5> data input; programmable weak pull-up. iocb5 1 i ttl interrupt-on-pin change. an11 1 i ana adc input channel 11. rx 1 i st asynchronous serial receive data input (usart module). dt 1 odig synchronous serial data output (usart module); takes priority over port data. 1 ist synchronous serial data input (usart module). user must configure as an input. rb6/iocb6/sck/ scl rb6 0 o dig latb<6> data output. 1 i ttl portb<6> data input; programmable weak pull-up. iocb6 1 i ttl interrupt-on-pin change. sck 0 o dig spi clock output (mssp module); takes priority over port data. 1 i st spi clock input (mssp module). scl 0 odigi 2 c? clock output (mssp module); takes priority over port data. 1 ii2ci 2 c? clock input (mssp module); input type depends on module setting. rb7/iocb7/tx/ck rb7 0 o dig latb<7> data output. 1 i ttl portb<7> data input; programmable weak pull-up. iocb7 1 i ttl interrupt-on-pin change. tx 1 odig asynchronous serial transmit data output (usart module); takes priority over port data. user must configure as output. ck 1 odig synchronous serial clock output (usart module); takes priority over port data. 1 i st synchronous serial clock input (usart module). legend: dig = digital level output; ttl = ttl input buffer; st = schmitt trigger input buffer; ana = analog level input/output; x = don?t care (tris bit does not affect port direction or is overridden for this option).
? 2010 microchip technology inc. preliminary ds41350e-page 93 pic18f/lf1xk50 table 9-4: summary of registers associated with portb name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page portb rb7 rb6 rb5 rb4 ? ? ? ?288 latb latb7 latb6 latb5 latb4 ? ? ? ?288 trisb trisb7 trisb6 trisb5 trisb4 ? ? ? ?288 wpub wpub7 wpub6 wpub5 wpub4 ? ? ? ?288 iocb iocb7 iocb6 iocb5 iocb4 288 slrcon ? ? ? ? ? slrc slrb slra 288 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 285 intcon2 rabpu intedg0 intedg1 intedg2 ? tmr0ip ?rabip285 anselh ? ? ? ? ans11 ans10 ans9 ans8 288 txsta csrc tx9 txen sync sendb brgh trmt tx9d 287 rcsta spen rx9 sren cren adden ferr oerr rx9d 287 sspcon1 wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 286 legend: ? = unimplemented, read as ? 0 ?. shaded cells are not used by portb.
pic18f/lf1xk50 ds41350e-page 94 preliminary ? 2010 microchip technology inc. 9.3 portc, trisc and latc registers portc is an 8-bit wide, bidirectional port. the corre- sponding data direction register is trisc. setting a trisc bit (= 1 ) will make the corresponding portc pin an input (i.e., disable the output driver). clearing a trisc bit (= 0 ) will make the corresponding portc pin an output (i.e., enable the output driver and put the contents of the output latch on the selected pin). the portc data latch register (latc) is also memory mapped. read-modify-write operations on the latc register read and write the latched output value for portc. all the pins on portc are implemented with schmitt trigger input buffer. each pin is individually configu- rable as an input or output. example 9-3: initializing portc note: on a power-on reset, rc<7:6> and rc<3:0> are configured as analog inputs and read as ? 0 ?. clrf portc ; initialize portc by ; clearing output ; data latches clrf latc ; alternate method ; to clear output ; data latches movlw 0cfh ; value used to ; initialize data ; direction movwf trisc ; set rc<3:0> as inputs ; rc<5:4> as outputs ; rc<7:6> as inputs register 9-11: portc: portc register r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x rc7 rc6 rc5 rc4 rc3 rc2 rc1 rc0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-0 rc<7:0> : portc i/o pin bit 1 = port pin is > v ih 0 = port pin is < v il register 9-12: trisc: po rtc tri-state register r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-0 trisc<7:0>: portc tri-state control bit 1 = portc pin configured as an input (tri-stated) 0 = portc pin configured as an output
? 2010 microchip technology inc. preliminary ds41350e-page 95 pic18f/lf1xk50 register 9-13: latc: portc data latch register r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x latc7 latc6 latc5 latc4 latc3 latc2 latc1 latc0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-0 latc<7:0> : rb<7:0> port i/o output latch register bits
pic18f/lf1xk50 ds41350e-page 96 preliminary ? 2010 microchip technology inc. table 9-14: portc i/o summary pin function tris setting i/o i/o type description rc0/an4/ c12in+/v ref +/ int0 rc0 0 o dig latc<0> data output. 1 i st portc<0> data input. an4 1 i ana a/d input channel 4. c12in+ 1 i ana comparators c1 and c2 non-inverting input. analog select is shared with adc . v ref + 1 i ana adc and comparator voltage reference high input. int0 1 i st external interrupt 0 input. rc1/an5/ c12in1-/v ref -/ int1 rc1 0 o dig latc<1> data output. 1 i st portc<1> data input. an5 1 i ana a/d input channel 5. c12in1- 1 i ana comparators c1 and c2 inverting input. analog select is shared with adc . v ref - 1 i ana adc and comparator voltage reference low input. int1 1 i st external interrupt 1 input. rc2/an6/ c12in2-/cv ref / p1d/int2 rc2 0 o dig latc<2> data output. 1 i st portc<2> data input. an6 1 i ana a/d input channel 6. c12in2- 1 i ana comparators c1 and c2 inverting input, channel 2. analog select is shared with adc . cv ref x o ana voltage reference output. enabling this feature disables digital i/o. p1d 0 o dig eccp1 enhanced pwm output, channel d. may be configured for tri-state during enhanced pwm shutdow n events. takes priority over port data. int2 1 i st external interrupt 2 input. rc3/an7/ c12in3-/p1c/ pgm rc3 0 o dig latc<3> data output. 1 i st portc<3> data input. an7 1 i ana a/d input channel 7. c12in3- 1 i ana comparators c1 and c2 inverting input, channel 3. analog select is shared with adc . p1c 0 o dig eccp1 enhanced pwm output, channel c. may be configured for tri-state during enhanced pwm shutdow n events. takes priority over port data. pgm x i st single-supply programming mode entry (icsp?). enabled by lvp configuration bit; all other pin functions disabled. rc4/c12out/ p1b rc4 0 o dig latc<4> data output. 1 i st portc<4> data input. c12out 0 o dig comparator 1 and 2 output; takes priority over port data. p1b 0 o dig eccp1 enhanced pwm output, channel b. may be configured for tri-state during enhanced pwm shutdow n events. takes priority over port data. legend: dig = digital level output; ttl = ttl input buffer; st = schmitt trigger input buffer; ana = analog level input/output; i 2 c/smb = i 2 c/smbus input buffer; x = don?t care (tris bit does not affect port direction or is overridden for this option).
? 2010 microchip technology inc. preliminary ds41350e-page 97 pic18f/lf1xk50 table 9-5: summary of registers associated with portc rc5/ccp1/p1a/ t0cki rc5 0 o dig latc<5> data output. 1 i st portc<5> data input. ccp1 0 o dig eccp1 compare or pwm output; takes priority over port data. 1 i st eccp1 capture input. p1a 0 0 dig eccp1 enhanced pwm output, channel a. may be configured for tri-state during enhanced pwm shut down events. takes priority over port data t0cki 1 i st timer0 counter input. rc6/an8/ss / t13cki/t1osci rc6 0 o dig latc<6> data output. 1 i st portc<6> data input. an8 1 i ana a/d input channel 8. ss 1 i ttl slave select input for ssp (mssp module) t13cki 1 i st timer1 and timer3 counter input. t1osci x o ana timer1 oscillator input; enabled when timer1 oscillator enabled. disables digital i/o. rc7/an9/sdo/ t1osco rc7 0 o dig latc<7> data output. 1 i st portc<7> data input. an9 1 i ana a/d input channel 9. sdo 0 i dig spi data output (mssp module); takes priority over port data. t1osco x o ana timer1 oscillator output; enabled when timer1 oscillator enabled. disables digital i/o. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page portc rc7 rc6 rc5 rc4 rc3 rc2 rc1 rc0 288 latc latc7 latc6 latc5 latc4 latc3 latc2 latc1 latc0 288 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 288 ansel ans7 ans6 ans5 ans4 ans3 ? ? ?288 anselh ? ? ? ? ans11 ans10 ans9 ans8 288 t1con rd16 t1run t1ckps1 t1ckps0 t1oscen t1sync tmr1cs tmr1on 286 t3con rd16 ? t3ckps1 t3ckps0 t3ccp1 t3sync tmr3cs tmr3on 287 sspcon1 wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 286 ccp1con p1m1 p1m0 dc1b1 dc1b0 ccp1m3 ccp1m2 ccp1m1 ccp1m0 287 eccp1as eccpase eccpas2 eccpas1 eccpas0 pssac1 pssac0 pssbd1 pssbd0 287 pstrcon ? ? ? strsync strd strc strb stra 287 slrcon ? ? ? ? ?slrc slrb slra 288 refcon1 d1en d1lps dac1oe --- d1pss1 d1pss0 --- d1nss 287 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 285 intcon2 rabpu intedg0 intedg1 intedg2 ? tmr0ip ? rabip 285 intcon3 int2ip int1ip ? int2ie int1ie ? int2if int1if 285 table 9-14: portc i/o summary (continued) pin function tris setting i/o i/o type description legend: dig = digital level output; ttl = ttl input buffer; st = schmitt trigger input buffer; ana = analog level input/output; i 2 c/smb = i 2 c/smbus input buffer; x = don?t care (tris bit does not affect port direction or is overridden for this option).
pic18f/lf1xk50 ds41350e-page 98 preliminary ? 2010 microchip technology inc. 9.4 port analog control some port pins are multiplexed with analog functions such as the analog-to-digital converter and compara- tors. when these i/o pins are to be used as analog inputs it is necessary to disable the digital input buffer to avoid excessive current caused by improper biasing of the digital input. individual control of the digital input buffers on pins which share analog functions is pro- vided by the ansel and anselh registers. setting an ansx bit high will disable the associated digital input buffer and cause all reads of that pin to return ? 0 ? while allowing analog functions of that pin to operate correctly. the state of the ansx bits has no affect on digital output functions. a pin with the associated trisx bit clear and ansx bit set will still operate as a digital output but the input mode will be analog. register 9-15: ansel: analog select register 1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 u-0 u-0 u-0 ans7 ans6 ans5 ans4 ans3 ? ? ? bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 ans7: rc3 analog select control bit 1 = digital input buffer of rc3 is disabled 0 = digital input buffer of rc3 is enabled bit 6 ans6: rc2 analog select control bit 1 = digital input buffer of rc2 is disabled 0 = digital input buffer of rc2 is enabled bit 5 ans5: rc1 analog select control bit 1 = digital input buffer of rc1 is disabled 0 = digital input buffer of rc1 is enabled bit 4 ans4: rc0 analog select control bit 1 = digital input buffer of rc0 is disabled 0 = digital input buffer of rc0 is enabled bit 3 ans3: ra4 analog select control bit 1 = digital input buffer of ra4 is disabled 0 = digital input buffer of ra4 is enabled bit 2-0 unimplemented: read as ? 0 ?
? 2010 microchip technology inc. preliminary ds41350e-page 99 pic18f/lf1xk50 register 9-16: anselh: analog select register 2 u-0 u-0 u-0 u-0 r/w-1 r/w-1 r/w-1 r/w-1 ? ? ? ? ans11 ans10 ans9 ans8 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-4 unimplemented: read as ? 0 ? bit 3 ans11: rb5 analog select control bit 1 = digital input buffer of rb5 is disabled 0 = digital input buffer of rb5 is enabled bit 2 ans10: rb4 analog select control bit 1 = digital input buffer of rb4 is disabled 0 = digital input buffer of rb4 is enabled bit 1 ans9: rc7 analog select control bit 1 = digital input buffer of rc7 is disabled 0 = digital input buffer of rc7 is enabled bit 0 ans8: rc6 analog select control bit 1 = digital input buffer of rc6 is disabled 0 = digital input buffer of rc6 is enabled
pic18f/lf1xk50 ds41350e-page 100 preliminary ? 2010 microchip technology inc. 9.5 port slew rate control the output slew rate of each port is programmable to select either the standard transition rate or a reduced transition rate of 0.1 times the standard to minimize emi. the reduced transition time is the default slew rate for all ports. register 9-17: slrcon: sl ew rate control register u-0 u-0 u-0 u-0 u-0 r/w-1 r/w-1 r/w-1 ? ? ? ? ? slrc slrb slra bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-3 unimplemented: read as ? 0 ? bit 2 slrc: portc slew rate control bit 1 = all outputs on portc slew at 0.1 times the standard rate 0 = all outputs on portc slew at the standard rate bit 1 slrb: portb slew rate control bit 1 = all outputs on portb slew at 0.1 times the standard rate 0 = all outputs on portb slew at the standard rate bit 0 slra: porta slew rate control bit 1 = all outputs on porta slew at 0.1 times the standard rate ( 1 ) 0 = all outputs on porta slew at the standard rate note 1: the slew rate of ra4 defaults to standard rate when the pin is used as clkout.
? 2010 microchip technology inc. preliminary ds41350e-page 101 pic18f/lf1xk50 10.0 timer0 module the timer0 module incorporates the following features: ? software selectable operation as a timer or coun- ter in both 8-bit or 16-bit modes ? readable and writable registers ? dedicated 8-bit, software programmable prescaler ? selectable clock source (internal or external) ? edge select for external clock ? interrupt-on-overflow the t0con register ( register 10-1 ) controls all aspects of the module?s operation, including the prescale selection. it is both readable and writable. a simplified block diagram of the timer0 module in 8-bit mode is shown in figure 10-1 . figure 10-2 shows a simplified block diagram of the timer0 module in 16-bit mode. register 10-1: t0con: ti mer0 control register r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 tmr0on t08bit t0cs t0se psa t0ps2 t0ps1 t0ps0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 tmr0on: timer0 on/off control bit 1 = enables timer0 0 = stops timer0 bit 6 t08bit : timer0 8-bit/16-bit control bit 1 = timer0 is configured as an 8-bit timer/counter 0 = timer0 is configured as a 16-bit timer/counter bit 5 t0cs : timer0 clock source select bit 1 = transition on t0cki pin 0 = internal instruction cycle clock (clkout) bit 4 t0se : timer0 source edge select bit 1 = increment on high-to-low transition on t0cki pin 0 = increment on low-to-high transition on t0cki pin bit 3 psa : timer0 prescaler assignment bit 1 = timer0 prescaler is not assigned. timer0 clock input bypasses prescaler. 0 = timer0 prescaler is assigned. timer0 clock input comes from prescaler output. bit 2-0 t0ps<2:0> : timer0 prescaler select bits 111 = 1:256 prescale value 110 = 1:128 prescale value 101 = 1:64 prescale value 100 = 1:32 prescale value 011 = 1:16 prescale value 010 = 1:8 prescale value 001 = 1:4 prescale value 000 = 1:2 prescale value
pic18f/lf1xk50 ds41350e-page 102 preliminary ? 2010 microchip technology inc. 10.1 timer0 operation timer0 can operate as either a timer or a counter; the mode is selected with the t0cs bit of the t0con register. in timer mode (t0cs = 0 ), the module increments on every clock by default unless a different prescaler value is selected (see section 10.3 ?prescaler? ). timer0 incrementing is inhibited for two instruction cycles following a tmr0 register write. the user can work around this by adjusting the value written to the tmr0 register to compensate for the anticipated missing increments. the counter mode is selected by setting the t0cs bit (= 1 ). in this mode, timer0 increments either on every rising or falling edge of the t0cki pin. the increment- ing edge is determined by the timer0 source edge select bit, t0se of the t0con register; clearing this bit selects the rising edge. restrictions on the external clock input are discussed below. an external clock source can be used to drive timer0; however, it must meet certain requirements (see table 27-6 ) to ensure that the external clock can be synchronized with the internal phase clock (t osc ). there is a delay between synchronization and the onset of incrementing the timer/counter. 10.2 timer0 reads and writes in 16-bit mode tmr0h is not the actual high byte of timer0 in 16-bit mode; it is actually a buffered version of the real high byte of timer0 which is neither directly readable nor writable (refer to figure 10-2 ). tmr0h is updated with the contents of the high byte of timer0 during a read of tmr0l. this provides the ability to read all 16 bits of timer0 without the need to verify that the read of the high and low byte were valid. invalid reads could otherwise occur due to a rollover between successive reads of the high and low byte. similarly, a write to the high byte of timer0 must also take place through the tmr0h buffer register. writing to tmr0h does not directly affect timer0. instead, the high byte of timer0 is updated with the contents of tmr0h when a write occurs to tmr0l. this allows all 16 bits of timer0 to be updated at once. figure 10-1: timer0 block diagram (8-bit mode) note: upon reset, timer0 is enabled in 8-bit mode with clock input from t0cki max. prescale. t0cki pin t0se 0 1 0 1 t0cs f osc /4 programmable prescaler sync with internal clocks tmr0l (2 t cy delay) internal data bus psa t0ps<2:0> set tmr0if on overflow 3 8 8
? 2010 microchip technology inc. preliminary ds41350e-page 103 pic18f/lf1xk50 figure 10-2: timer0 block di agram (16-bit mode) 10.3 prescaler an 8-bit counter is available as a prescaler for the timer0 module. the prescaler is not directly readable or writable; its value is set by the psa and t0ps<2:0> bits of the t0con register which determine the prescaler assignment and prescale ratio. clearing the psa bit assigns the prescaler to the timer0 module. when the prescaler is assigned, prescale values from 1:2 through 1:256 in integer power-of-2 increments are selectable. when assigned to the timer0 module, all instructions writing to the tmr0 register (e.g., clrf tmr0 , movwf tmr0 , bsf tmr0 , etc.) clear the prescaler count. 10.3.1 switching prescaler assignment the prescaler assignment is fully under software control and can be changed ?on-the-fly? during program execution. 10.4 timer0 interrupt the tmr0 interrupt is generated when the tmr0 reg- ister overflows from ffh to 00h in 8-bit mode, or from ffffh to 0000h in 16-bit mode. this overflow sets the tmr0if flag bit. the interrupt can be masked by clear- ing the tmr0ie bit of the intcon register. before re-enabling the interrupt, the tmr0if bit must be cleared by software in the interrupt service routine. since timer0 is shut down in sleep mode, the tmr0 interrupt cannot awaken the processor from sleep. table 10-1: registers associated with timer0 note: upon reset, timer0 is enabled in 8-bit mode with clock input from t0cki max. prescale. t0cki pin t0se 0 1 0 1 t0cs f osc /4 programmable prescaler sync with internal clocks tmr0l (2 t cy delay) internal data bus 8 psa t0ps<2:0> set tmr0if on overflow 3 tmr0 tmr0h high byte 8 8 8 read tmr0l write tmr0l 8 note: writing to tmr0 when the prescaler is assigned to timer0 will clear the prescaler count but will not change the prescaler assignment. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page tmr0l timer0 register, low byte 286 tmr0h timer0 register, high byte 286 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 285 t0con tmr0on t08bit t0cs t0se psa t0ps2 t0ps1 t0ps0 286 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 288 legend: shaded cells are not used by timer0. note 1: porta<7:6> and their direction bits are individually configured as port pins based on various primary oscillator modes. when disabled, these bits read as ? 0 ?.
pic18f/lf1xk50 ds41350e-page 104 preliminary ? 2010 microchip technology inc. notes:
? 2010 microchip technology inc. preliminary ds41350e-page 105 pic18f/lf1xk50 11.0 timer1 module the timer1 timer/counter module incorporates the following features: ? software selectable operation as a 16-bit timer or counter ? readable and writable 8-bit registers (tmr1h and tmr1l) ? selectable internal or external clock source and timer1 oscillator options ? interrupt-on-overflow ? reset on ccp special event trigger ? device clock status flag (t1run) a simplified block diagram of the timer1 module is shown in figure 11-1 . a block diagram of the module?s operation in read/write mode is shown in figure 11-2 . the module incorporates its own low-power oscillator to provide an additional clocking option. the timer1 oscillator can also be used as a low-power clock source for the microcontroller in power-managed operation. timer1 can also be used to provide real-time clock (rtc) functionality to applications with only a minimal addition of external components and code overhead. timer1 is controlled through the t1con control register ( register 11-1 ). it also contains the timer1 oscillator enable bit (t1oscen). timer1 can be enabled or disabled by setting or clearing control bit, tmr1on of the t1con register. register 11-1: t1con: timer1 control register r/w-0 r-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 rd16 t1run t1ckps1 t1ckps0 t1oscen t1sync tmr1cs tmr1on bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 rd16: 16-bit read/write mode enable bit 1 = enables register read/write of timer1 in one 16-bit operation 0 = enables register read/write of timer1 in two 8-bit operations bit 6 t1run: timer1 system clock status bit 1 = main system clock is derived from timer1 oscillator 0 = main system clock is derived from another source bit 5-4 t1ckps<1:0>: timer1 input clock prescale select bits 11 = 1:8 prescale value 10 = 1:4 prescale value 01 = 1:2 prescale value 00 = 1:1 prescale value bit 3 t1oscen: timer1 oscillator enable bit 1 = timer1 oscillator is enabled 0 = timer1 oscillator is shut off the oscillator inverter and feedback resistor are turned off to eliminate power drain. bit 2 t1sync : timer1 external clock input synchronization select bit when tmr1cs = 1 : 1 = do not synchronize external clock input 0 = synchronize external clock input when tmr1cs = 0 : this bit is ignored. timer1 uses the internal clock when tmr1cs = 0 . bit 1 tmr1cs: timer1 clock source select bit 1 = external clock from the t13cki pin (on the rising edge) 0 = internal clock (f osc /4) bit 0 tmr1on: timer1 on bit 1 = enables timer1 0 = stops timer1
pic18f/lf1xk50 ds41350e-page 106 preliminary ? 2010 microchip technology inc. 11.1 timer1 operation timer1 can operate in one of the following modes: ?timer ? synchronous counter ? asynchronous counter the operating mode is determined by the clock select bit, tmr1cs of the t1con register. when tmr1cs is cleared (= 0 ), timer1 increments on every internal instruction cycle (f osc /4). when the bit is set, timer1 increments on every rising edge of either the timer1 external clock input or the timer1 oscillator, if enabled. when the timer1 oscillator is enabled, the digital circuitry associated with the t1osi and t1oso pins is disabled. this means the values of trisc<1:0> are ignored and the pins are read as ? 0 ?. figure 11-1: timer1 block diagram figure 11-2: timer1 block diagram (16-bit read/write mode) t1sync tmr1cs t1ckps<1:0> sleep input t1oscen (1) f osc /4 internal clock on/off prescaler 1, 2, 4, 8 synchronize detect 1 0 2 t1osi/t13cki t1oso 1 0 tmr1on tmr1l set tmr1if on overflow tmr1 high byte clear tmr1 (ccp special event trigger) timer1 oscillator note 1: when enable bit, t1oscen, is cleared, the inverter and f eedback resistor are turned off to eliminate power drain. on/off timer1 timer1 clock input t1sync tmr1cs t1ckps<1:0> sleep input t1oscen (1) f osc /4 internal clock prescaler 1, 2, 4, 8 synchronize detect 1 0 2 t1osi/t13cki t1oso note 1: when enable bit, t1oscen, is cleared, the inverter and fee dback resistor are turned off to eliminate power drain. 1 0 tmr1l internal data bus 8 set tmr1if on overflow tmr1 tmr1h high byte 8 8 8 read tmr1l write tmr1l 8 tmr1on clear tmr1 (ccp special event trigger) timer1 oscillator on/off timer1 timer1 clock input
? 2010 microchip technology inc. preliminary ds41350e-page 107 pic18f/lf1xk50 11.2 timer1 16-bit read/write mode timer1 can be configured for 16-bit reads and writes (see figure 11-2 ). when the rd16 control bit of the t1con register is set, the address for tmr1h is mapped to a buffer register for the high byte of timer1. a read from tmr1l will load the contents of the high byte of timer1 into the timer1 high byte buffer. this provides the user with the ability to accurately read all 16 bits of timer1 without the need to determine whether a read of the high byte, followed by a read of the low byte, has become invalid due to a rollover or carry between reads. writing to tmr1h does not directly affect timer1. instead, the high byte of timer1 is updated with the contents of tmr1h when a write occurs to tmr1l. this allows all 16 bits of timer1 to be updated at once. the high byte of timer1 is not directly readable or writable in this mode. all reads and writes must take place through the timer1 high byte buffer register. writes to tmr1h do not clear the timer1 prescaler. the prescaler is only cleared on writes to tmr1l. 11.3 timer1 oscillator an on-chip crystal oscillator circuit is incorporated between pins t1osi (input) and t1oso (amplifier output). it is enabled by setting the timer1 oscillator enable bit, t1oscen of the t1con register. the oscillator is a low-power circuit rated for 32 khz crystals. it will continue to run during all power-managed modes. the circuit for a typical lp oscillator is shown in figure 11-3 . table 11-1 shows the capacitor selection for the timer1 oscillator. the user must provide a software time delay to ensure proper start-up of the timer1 oscillator. figure 11-3: external components for the timer1 lp oscillator table 11-1: capacitor selection for the timer oscillator 11.3.1 using timer1 as a clock source the timer1 oscillator is also available as a clock source in power-managed modes. by setting the clock select bits, scs<1:0> of the osccon register, to ? 01 ?, the device switches to sec_run mode; both the cpu and peripherals are clocked from the timer1 oscillator. if the idlen bit of the osccon register is cleared and a sleep instruction is executed, the device enters sec_idle mode. additional details are available in section 19.0 ?power-managed modes? . whenever the timer1 oscillator is providing the clock source, the timer1 system clock status flag, t1run of the t1con register, is set. this can be used to deter- mine the controller?s current clocking mode. it can also indicate which clock source is currently being used by the fail-safe clock monitor. if the clock monitor is enabled and the timer1 oscillator fails while providing the clock, polling the t1run bit will indicate whether the clock is being provided by the timer1 oscillator or another source. note: see the notes with table 11-1 for additional information about capacitor selection. c1 c2 xtal t1osi t1oso 32.768 khz 27 pf 27 pf pic ? mcu osc type freq c1 c2 lp 32 khz 27 pf (1) 27 pf (1) note 1: microchip suggests these values only as a starting point in validating the oscillator circuit. 2: higher capacitance increases the stabil- ity of the oscillator but also increases the start-up time. 3: since each resonator/crystal has its own characteristics, the user should consult the resonator/crystal manufacturer for appropriate values of external components. 4: capacitor values are for design guidance only.
pic18f/lf1xk50 ds41350e-page 108 preliminary ? 2010 microchip technology inc. 11.3.2 timer1 oscillator layout considerations the timer1 oscillator circuit draws very little power during operation. due to the low-power nature of the oscillator, it may also be sensitive to rapidly changing signals in close proximity. the oscillator circuit, shown in figure 11-3 , should be located as close as possible to the microcontroller. there should be no circuits passing within the oscillator circuit boundaries other than v ss or v dd . if a high-speed circuit must be located near the oscilla- tor (such as the ccp1 pin in output compare or pwm mode, or the primary oscillator using the osc2 pin), a grounded guard ring around the oscillator circuit, as shown in figure 11-4 , may be helpful when used on a single-sided pcb or in addition to a ground plane. figure 11-4: oscillator circuit with grounded guard ring 11.4 timer1 interrupt the tmr1 register pair (tmr1h:tmr1l) increments from 0000h to ffffh and rolls over to 0000h. the timer1 interrupt, if enabled, is generated on overflow, which is latched in the tmr1if interrupt flag bit of the pir1 register. this interrupt can be enabled or disabled by setting or clearing the tmr1ie interrupt enable bit of the pie1 register. 11.5 resetting timer1 using the ccp special event trigger if either of the ccp modules is configured to use timer1 and generate a special event trigger in compare mode (ccp1m<3:0> or ccp2m<3:0> = 1011 ), this signal will reset timer1. the trigger from ccp2 will also start an a/d conversion if the a/d module is enabled (see section 14.3.4 ?special event trigger? for more information). the module must be configured as either a timer or a synchronous counter to take advantage of this feature. when used this way, the ccprh:ccprl register pair effectively becomes a period register for timer1. if timer1 is running in asynchronous counter mode, this reset operation may not work. in the event that a write to timer1 coincides with a special event trigger, the write operation will take precedence. v dd osc1 v ss osc2 rc0 rc1 rc2 note: not drawn to scale. note: the special event triggers from the ccp2 module will not set the tmr1if interrupt flag bit of the pir1 register.
? 2010 microchip technology inc. preliminary ds41350e-page 109 pic18f/lf1xk50 11.6 using timer1 as a real-time clock adding an external lp oscillator to timer1 (such as the one described in section 11.3 ?timer1 oscillator? above) gives users the option to include rtc function- ality to their applications. this is accomplished with an inexpensive watch crystal to provide an accurate time base and several lines of application code to calculate the time. when operating in sleep mode and using a battery or supercapacitor as a power source, it can completely eliminate the need for a separate rtc device and battery backup. the application code routine, rtcisr , shown in example 11-1 , demonstrates a simple method to increment a counter at one-second intervals using an interrupt service routine. incrementing the tmr1 register pair to overflow triggers the interrupt and calls the routine, which increments the seconds counter by one; additional counters for minutes and hours are incremented on overflows of the less significant counters. since the register pair is 16 bits wide, a 32.768 khz clock source will take 2 seconds to count up to over- flow. to force the overflow at the required one-second intervals, it is necessary to preload it; the simplest method is to set the msb of tmr1h with a bsf instruc- tion. note that the tmr1l register is never preloaded or altered; doing so may introduce cumulative error over many cycles. for this method to be accurate, timer1 must operate in asynchronous mode and the timer1 overflow interrupt must be enabled (pie1<0> = 1 ), as shown in the routine, rtcinit . the timer1 oscillator must also be enabled and running at all times. example 11-1: implementing a real-time clock using a timer1 interrupt service rtcinit movlw 80h ; preload tmr1 register pair movwf tmr1h ; for 1 second overflow clrf tmr1l movlw b?00001111? ; configure for external clock, movwf t1con ; asynchronous operation, external oscillator clrf secs ; initialize timekeeping registers clrf mins ; movlw .12 movwf hours bsf pie1, tmr1ie ; enable timer1 interrupt return rtcisr bsf tmr1h, 7 ; preload for 1 sec overflow bcf pir1, tmr1if ; clear interrupt flag incf secs, f ; increment seconds movlw .59 ; 60 seconds elapsed? cpfsgt secs return ; no, done clrf secs ; clear seconds incf mins, f ; increment minutes movlw .59 ; 60 minutes elapsed? cpfsgt mins return ; no, done clrf mins ; clear minutes incf hours, f ; increment hours movlw .23 ; 24 hours elapsed? cpfsgt hours return ; no, done clrf hours ; reset hours return ; done
pic18f/lf1xk50 ds41350e-page 110 preliminary ? 2010 microchip technology inc. table 11-2: registers associated with timer1 as a timer/counter name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 285 pir1 ? adif rcif txif sspif ccp1if tmr2if tmr1if 288 pie1 ? adie rcie txie sspie ccp1ie tmr2ie tmr1ie 288 ipr1 ? adip rcip txip sspip ccp1ip tmr2ip tmr1ip 288 tmr1l timer1 register, low byte 286 tmr1h timer1 register, high byte 286 t1con rd16 t1run t1ckps1 t1ckps0 t1oscen t1sync tmr1cs tmr1on 286 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 288 anselh ? ? ? ? ans11 ans10 ans9 ans8 288 sspcon1 wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 286 legend: ? = unimplemented, read as ? 0 ?. shaded cells are not used by the timer1 module.
? 2010 microchip technology inc. preliminary ds41350e-page 111 pic18f/lf1xk50 12.0 timer2 module the timer2 module timer incorporates the following features: ? 8-bit timer and period registers (tmr2 and pr2, respectively) ? readable and writable (both registers) ? software programmable prescaler (1:1, 1:4 and 1:16) ? software programmable postscaler (1:1 through 1:16) ? interrupt on tmr2-to-pr2 match ? optional use as the shift clock for the mssp module the module is controlled through the t2con register ( register 12-1 ), which enables or disables the timer and configures the prescaler and postscaler. timer2 can be shut off by clearing control bit, tmr2on of the t2con register, to minimize power consumption. a simplified block diagram of the module is shown in figure 12-1 . 12.1 timer2 operation in normal operation, tmr2 is incremented from 00h on each clock (f osc /4). a 4-bit counter/prescaler on the clock input gives direct input, divide-by-4 and divide-by-16 prescale options; these are selected by the prescaler control bits, t2ckps<1:0> of the t2con register. the value of tmr2 is compared to that of the period register, pr2, on each clock cycle. when the two values match, the comparator generates a match signal as the timer output. this signal also resets the value of tmr2 to 00h on the next cycle and drives the output counter/postscaler (see section 12.2 ?timer2 interrupt? ). the tmr2 and pr2 registers are both directly readable and writable. the tmr2 register is cleared on any device reset, whereas the pr2 register initializes to ffh. both the prescaler and postscaler counters are cleared on the following events: ? a write to the tmr2 register ? a write to the t2con register ? any device reset (power-on reset, mclr reset, watchdog timer reset or brown-out reset) tmr2 is not cleared when t2con is written. register 12-1: t2con: ti mer2 control register u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ? t2outps3 t2outps2 t2outps1 t 2outps0 tmr2on t2ckps1 t2ckps0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 unimplemented: read as ? 0 ? bit 6-3 t2outps<3:0> : timer2 output postscale select bits 0000 = 1:1 postscale 0001 = 1:2 postscale ? ? ? 1111 = 1:16 postscale bit 2 tmr2on : timer2 on bit 1 = timer2 is on 0 = timer2 is off bit 1-0 t2ckps<1:0> : timer2 clock prescale select bits 00 = prescaler is 1 01 = prescaler is 4 1x = prescaler is 16
pic18f/lf1xk50 ds41350e-page 112 preliminary ? 2010 microchip technology inc. 12.2 timer2 interrupt timer2 can also generate an optional device interrupt. the timer2 output signal (tmr2-to-pr2 match) pro- vides the input for the 4-bit output counter/postscaler. this counter generates the tmr2 match interrupt flag which is latched in tmr2if of the pir1 register. the interrupt is enabled by setting the tmr2 match inter- rupt enable bit, tmr2ie of the pie1 register. a range of 16 postscale options (from 1:1 through 1:16 inclusive) can be selected with the postscaler control bits, t2outps<3:0> of the t2con register. 12.3 timer2 output the unscaled output of tmr2 is available primarily to the ccp modules, where it is used as a time base for operations in pwm mode. timer2 can be optionally used as the shift clock source for the mssp module operating in spi mode. addi- tional information is provided in section 14.0 ?master synchronous serial port (mssp) module? . figure 12-1: timer2 block diagram table 12-1: registers associated with timer2 as a timer/counter name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 285 pir1 ? adif rcif txif sspif ccp1if tmr2if tmr1if 288 pie1 ? adie rcie txie sspie ccp1ie tmr2ie tmr1ie 288 ipr1 ? adip rcip txip sspip ccp1ip tmr2ip tmr1ip 288 tmr2 timer2 register 286 t2con ? t2outps3 t2outps2 t2outps1 t2o utps0 tmr2on t2ckps1 t2ckps0 286 pr2 timer2 period register 286 legend: ? = unimplemented, read as ? 0 ?. shaded cells are not used by the timer2 module. comparator tmr2 output tmr2 postscaler prescaler pr2 2 f osc /4 1:1 to 1:16 1:1, 1:4, 1:16 4 t2outps<3:0> t2ckps<1:0> set tmr2if internal data bus 8 reset tmr2/pr2 8 8 (to pwm or mssp) match
? 2010 microchip technology inc. preliminary ds41350e-page 113 pic18f/lf1xk50 13.0 timer3 module the timer3 module timer/counter incorporates these features: ? software selectable operation as a 16-bit timer or counter ? readable and writable 8-bit registers (tmr3h and tmr3l) ? selectable clock source (internal or external) with device clock or timer1 oscillator internal options ? interrupt-on-overflow ? module reset on ccp special event trigger a simplified block diagram of the timer3 module is shown in figure 13-1 . a block diagram of the module?s operation in read/write mode is shown in figure 13-2 . the timer3 module is controlled through the t3con register ( register 13-1 ). it also selects the clock source options for the ccp modules (see section 14.1.1 ?ccp module and timer resources? for more information). register 13-1: t3con: ti mer3 control register r/w-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 rd16 ? t3ckps1 t3ckps0 t3ccp1 t3sync tmr3cs tmr3on bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 rd16: 16-bit read/write mode enable bit 1 = enables register read/write of timer3 in one 16-bit operation 0 = enables register read/write of timer3 in two 8-bit operations bit 6 unimplemented: read as ? 0 ? bit 5-4 t3ckps<1:0> : timer3 input clock prescale select bits 11 = 1:8 prescale value 10 = 1:4 prescale value 01 = 1:2 prescale value 00 = 1:1 prescale value bit 3 t3ccp1: timer3 and timer1 to ccp1 enable bits 1 = timer3 is the clock source for compare/capture of eccp1 0 = timer1 is the clock source for compare/capture of eccp1 bit 2 t3sync : timer3 external clock input synchronization control bit (not usable if the device clock comes from timer1/timer3.) when tmr3cs = 1 : 1 = do not synchronize external clock input 0 = synchronize external clock input when tmr3cs = 0 : this bit is ignored. timer3 uses the internal clock when tmr3cs = 0 . bit 1 tmr3cs: timer3 clock source select bit 1 = external clock input from timer1 oscillator or t13cki (on the rising edge after the first falling edge) 0 = internal clock (f osc /4) bit 0 tmr3on: timer3 on bit 1 = enables timer3 0 = stops timer3
pic18f/lf1xk50 ds41350e-page 114 preliminary ? 2010 microchip technology inc. 13.1 timer3 operation timer3 can operate in one of three modes: ?timer ? synchronous counter ? asynchronous counter the operating mode is determined by the clock select bit, tmr3cs of the t3con register. when tmr3cs is cleared (= 0 ), timer3 increments on every internal instruction cycle (f osc /4). when the bit is set, timer3 increments on every rising edge of the timer1 external clock input or the timer1 oscillator, if enabled. as with timer1, the digital circuitry associated with the rc1/t1osi and rc0/t1oso/t13cki pins is disabled when the timer1 oscillator is enabled. this means the values of trisc<1:0> are ignored and the pins are read as ? 0 ?. figure 13-1: timer3 block diagram t3sync tmr3cs t3ckps<1:0> sleep input t1oscen (1) f osc /4 internal clock prescaler 1, 2, 4, 8 synchronize detect 1 0 2 t1oso/t13cki t1osi 1 0 tmr3on tmr3l set tmr3if on overflow tmr3 high byte timer1 oscillator note 1: when enable bit, t1oscen, is cleared, the inverter and feedback resistor are turned off to eliminate power drain. on/off timer3 ccp1 special event trigger ccp1 select from t3con<3> clear tmr3 timer1 clock input
? 2010 microchip technology inc. preliminary ds41350e-page 115 pic18f/lf1xk50 figure 13-2: timer3 block diag ram (16-bit read/write mode) 13.2 timer3 16-bit read/write mode timer3 can be configured for 16-bit reads and writes (see figure 13-2 ). when the rd16 control bit of the t3con register is set, the address for tmr3h is mapped to a buffer register for the high byte of timer3. a read from tmr3l will load the contents of the high byte of timer3 into the timer3 high byte buffer register. this provides the user with the ability to accurately read all 16 bits of timer1 without having to determine whether a read of the high byte, followed by a read of the low byte, has become invalid due to a rollover between reads. a write to the high byte of timer3 must also take place through the tmr3h buffer register. the timer3 high byte is updated with the contents of tmr3h when a write occurs to tmr3l. this allows a user to write all 16 bits to both the high and low bytes of timer3 at once. the high byte of timer3 is not directly readable or writable in this mode. all reads and writes must take place through the timer3 high byte buffer register. writes to tmr3h do not clear the timer3 prescaler. the prescaler is only cleared on writes to tmr3l. 13.3 using the timer1 oscillator as the timer3 clock source the timer1 internal oscillator may be used as the clock source for timer3. the timer1 oscillator is enabled by setting the t1oscen bit of the t1con register. to use it as the timer3 clock source, the tmr3cs bit must also be set. as previously noted, this also configures timer3 to increment on every rising edge of the oscillator source. the timer1 oscillator is described in section 11.0 ?timer1 module? . 13.4 timer3 interrupt the tmr3 register pair (tmr3h:tmr3l) increments from 0000h to ffffh and overflows to 0000h. the timer3 interrupt, if enabled, is generated on overflow and is latched in interrupt flag bit, tmr3if of the pir2 register. this interrupt can be enabled or disabled by setting or clearing the timer3 interrupt enable bit, tmr3ie of the pie2 register. t3sync tmr3cs t3ckps<1:0> sleep input t1oscen (1) f osc /4 internal clock prescaler 1, 2, 4, 8 synchronize detect 1 0 2 t13cki/t1osi t1oso note 1: when enable bit, t1oscen, is cleared, the inverter and feedback resistor are turned off to eliminate power drain. 1 0 tmr3l internal data bus 8 set tmr3if on overflow tmr3 tmr3h high byte 8 8 8 read tmr1l write tmr1l 8 tmr3on ccp1 special event trigger timer1 oscillator on/off timer3 timer1 clock input ccp1 select from t3con<3> clear tmr3
pic18f/lf1xk50 ds41350e-page 116 preliminary ? 2010 microchip technology inc. 13.5 resetting timer3 using the ccp special event trigger if ccp1 module is configured to use timer3 and to gen- erate a special event trigger in compare mode (ccp1m<3:0>), this signal will reset timer3. it will also start an a/d conversion if the a/d module is enabled (see section 17.2.8 ?special event trigger? for more information). the module must be configured as either a timer or synchronous counter to take advantage of this feature. when used this way, the ccpr1h:ccpr1l register pair effectively becomes a period register for timer3. if timer3 is running in asynchronous counter mode, the reset operation may not work. in the event that a write to timer3 coincides with a special event trigger from a ccp module, the write will take precedence. table 13-1: registers associated with timer3 as a timer/counter name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 285 pir2 oscfif c1if c2if eeif bclif usbif tmr3if ccp2if 288 pie2 oscfie c1ie c2ie eeie bclie usbie tmr3ie ccp2ie 288 ipr2 oscfip c1ip c2ip eeip bclip usbip tmr3ip ccp2ip 288 tmr3l timer3 register, low byte 287 tmr3h timer3 register, high byte 287 t1con rd16 t1run t1ckps1 t1ckps0 t1oscen t1sync tmr1cs tmr1on 286 t3con rd16 ? t3ckps1 t3ckps0 t3ccp1 t3sync tmr3cs tmr3on 287 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 288 anselh ? ? ? ? ans11 ans10 ans9 ans8 288 legend: ? = unimplemented, read as ? 0 ?. shaded cells are not used by the timer3 module.
? 2010 microchip technology inc. preliminary ds41350e-page 117 pic18f/lf1xk50 14.0 enhanced capture/compare/pwm (eccp) module pic18f/lf1xk50 devices have one eccp (capture/compare/pwm) module. the module contains a 16-bit register which can operate as a 16-bit capture register, a 16-bit compare register or a pwm master/slave duty cycle register. ccp1 is implemented as a standard ccp module with enhanced pwm capabilities. these include: ? provision for 2 or 4 output channels ? output steering ? programmable polarity ? programmable dead-band control ? automatic shutdown and restart. the enhanced features are discussed in detail in section 14.4 ?pwm (enhanced mode)? . register 14-1: ccp1con: enhanced capt ure/compare/pwm control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 p1m1 p1m0 dc1b1 dc1b0 ccp1m3 ccp1m2 ccp1m1 ccp1m0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-6 p1m<1:0>: enhanced pwm output configuration bits if ccp1m<3:2> = 00 , 01 , 10 : xx = p1a assigned as capture/compare input/output; p1b, p1c, p1d assigned as port pins if ccp1m<3:2> = 11 : 00 = single output: p1a, p1b, p1c and p1d controlled by steering (see section 14.4.7 ?pulse steering mode? ). 01 = full-bridge output forward: p1d modulated; p1a active; p1b, p1c inactive 10 = half-bridge output: p1a, p1b modulated with dead-band control; p1c, p1d assigned as port pins 11 = full-bridge output reverse: p1b modulated; p1c active; p1a, p1d inactive bit 5-4 dc1b<1:0> : pwm duty cycle bit 1 and bit 0 capture mode: unused. compare mode: unused. pwm mode: these bits are the two lsbs of the 10-bit pwm duty cycle. the eight msbs of the duty cycle are found in ccpr1l. bit 3-0 ccp1m<3:0> : enhanced ccp mode select bits 0000 = capture/compare/pwm off (resets eccp module) 0001 = reserved 0010 = compare mode, toggle output on match 0011 = reserved 0100 = capture mode, every falling edge 0101 = capture mode, every rising edge 0110 = capture mode, every 4th rising edge 0111 = capture mode, every 16th rising edge 1000 = compare mode, initialize ccp1 pin low, set output on compare match (set ccp1if) 1001 = compare mode, initialize ccp1 pin high, clear output on compare match (set ccp1if) 1010 = compare mode, generate software interrupt only, ccp1 pin reverts to i/o state 1011 = compare mode, trigger special event (eccp resets tmr1 or tmr3, start a/d conversion, sets cc1if bit) 1100 = pwm mode; p1a, p1c active-high; p1b, p1d active-high 1101 = pwm mode; p1a, p1c active-high; p1b, p1d active-low 1110 = pwm mode; p1a, p1c active-low; p1b, p1d active-high 1111 = pwm mode; p1a, p1c active-low; p1b, p1d active-low
pic18f/lf1xk50 ds41350e-page 118 preliminary ? 2010 microchip technology inc. in addition to the expanded range of modes available through the ccp1con register and eccp1as register, the eccp module has two additional registers associated with enhanced pwm operation and auto-shutdown features. they are: ? pwm1con (dead-band delay) ? pstrcon (output steering) 14.1 eccp outputs and configuration the enhanced ccp module may have up to four pwm outputs, depending on the selected operating mode. these outputs, designated p1a through p1d, are multiplexed with i/o pins on portc. the outputs that are active depend on the ccp operating mode selected. the pin assignments are summarized in table 14-2 . to configure the i/o pins as pwm outputs, the proper pwm mode must be selected by setting the p1m<1:0> and ccp1m<3:0> bits. the appropriate trisc direction bits for the port pins must also be set as outputs. 14.1.1 ccp module and timer resources the ccp modules utilize timers 1, 2 or 3, depending on the mode selected. timer1 and timer3 are available to modules in capture or compare modes, while timer2 is available for modules in pwm mode. table 14-1: ccp mode ? timer resource the assignment of a particular timer to a module is determined by the timer-to-ccp enable bits in the t3con register ( register 13-1 ). the interactions between the two modules are summarized in figure 14-1 . in asynchronous counter mode, the capture operation will not work reliably. ccp/eccp mode timer resource capture timer1 or timer3 compare timer1 or timer3 pwm timer2
? 2010 microchip technology inc. preliminary ds41350e-page 119 pic18f/lf1xk50 14.2 capture mode in capture mode, the ccpr1h:ccpr1l register pair captures the 16-bit value of the tmr1 or tmr3 registers when an event occurs on the corresponding ccp1 pin. an event is defined as one of the following: ? every falling edge ? every rising edge ? every 4th rising edge ? every 16th rising edge the event is selected by the mode select bits, ccp1m<3:0> of the ccp1con register. when a cap- ture is made, the interrupt request flag bit, ccp1if, is set; it must be cleared by software. if another capture occurs before the value in register ccpr1 is read, the old captured value is overwritten by the new captured value. 14.2.1 ccp pin configuration in capture mode, the appropriate ccp1 pin should be configured as an input by setting the corresponding tris direction bit. 14.2.2 timer1/timer3 mode selection the timers that are to be used with the capture feature (timer1 and/or timer3) must be running in timer mode or synchronized counter mode. in asynchronous counter mode, the capture operation may not work. the timer to be used with each ccp module is selected in the t3con register (see section 14.1.1 ?ccp module and timer resources? ). 14.2.3 software interrupt when the capture mode is changed, a false capture interrupt may be generated. the user should keep the ccp1ie interrupt enable bit clear to avoid false inter- rupts. the interrupt flag bit, ccp1if, should also be cleared following any such change in operating mode. 14.2.4 ccp prescaler there are four prescaler settings in capture mode; they are specified as part of the operating mode selected by the mode select bits (ccp1m<3:0>). whenever the ccp module is turned off or capture mode is disabled, the prescaler counter is cleared. this means that any reset will clear the prescaler counter. switching from one capture prescaler to another may generate an interrupt. also, the prescaler counter will not be cleared; therefore, the first capture may be from a non-zero prescaler. example 14-1 shows the recommended method for switching between capture prescalers. this example also clears the prescaler counter and will not generate the ?false? interrupt. example 14-1: changing between capture prescalers figure 14-1: capture mode operat ion block diagram note: if the ccp1 pin is configured as an output, a write to the port can cause a capture condition. clrf ccp1con ; turn ccp module off movlw new_capt_ps ; load wreg with the ; new prescaler mode ; value and ccp on movwf ccp1con ; load ccp1con with ; this value ccpr1h ccpr1l tmr1h tmr1l set ccp1if tmr3 enable q1:q4 ccp1con<3:0> ccp1 pin prescaler ? 1, 4, 16 and edge detect tmr1 enable t3ccp1 t3ccp1 tmr3h tmr3l 4 4
pic18f/lf1xk50 ds41350e-page 120 preliminary ? 2010 microchip technology inc. 14.3 compare mode in compare mode, the 16-bit ccpr1 register value is constantly compared against either the tmr1 or tmr3 register pair value. when a match occurs, the ccp1 pin can be: ? driven high ? driven low ? toggled (high-to-low or low-to-high) ? remain unchanged (that is, reflects the state of the i/o latch) the action on the pin is based on the value of the mode select bits (ccp1m<3:0>). at the same time, the inter- rupt flag bit, ccp1if, is set. 14.3.1 ccp pin configuration the user must configure the ccp1 pin as an output by clearing the appropriate tris bit. 14.3.2 timer1/timer3 mode selection timer1 and/or timer3 must be running in timer mode or synchronized counter mode if the ccp module is using the compare feature. in asynchronous counter mode, the compare operation will not work reliably. 14.3.3 software interrupt mode when the generate software interrupt mode is chosen (ccp1m<3:0> = 1010 ), the ccp1 pin is not affected. only the ccp1if interrupt flag is affected. 14.3.4 special event trigger the ccp module is equipped with a special event trig- ger. this is an internal hardware signal generated in compare mode to trigger actions by other modules. the special event trigger is enabled by selecting the compare special event trigger mode (ccp1m<3:0> = 1011 ). the special event trigger resets the timer register pair for whichever timer resource is currently assigned as the module?s time base. this allows the ccpr1 registers to serve as a programmable period register for either timer. the special event trigger can also start an a/d conver- sion. in order to do this, the a/d converter must already be enabled. figure 14-2: compare mode operat ion block diagram note: clearing the ccp1con register will force the ccp1 compare output latch (depend- ing on device configuration) to the default low level. this is not the portc i/o data latch. tmr1h tmr1l tmr3h tmr3l ccpr1h ccpr1l comparator t3ccp 1 set ccp1if 1 0 q s r output logic special event trigger ccp1 pin tris ccp1con<3:0> output enable 4 (timer1/timer3 reset, a/d trigger) compare match
? 2010 microchip technology inc. preliminary ds41350e-page 121 pic18f/lf1xk50 14.4 pwm (enhanced mode) the enhanced pwm mode can generate a pwm signal on up to four different output pins with up to 10-bits of resolution. it can do this through four different pwm output modes: ? single pwm ? half-bridge pwm ? full-bridge pwm, forward mode ? full-bridge pwm, reverse mode to select an enhanced pwm mode, the p1m bits of the ccp1con register must be set appropriately. the pwm outputs are multiplexed with i/o pins and are designated p1a, p1b, p1c and p1d. the polarity of the pwm pins is configurable and is selected by setting the ccp1m bits in the ccp1con register appropriately. table 14-1 shows the pin assignments for each enhanced pwm mode. figure 14-3 shows an example of a simplified block diagram of the enhanced pwm module. figure 14-3: example simplified block diagram of the enhanced pwm mode table 14-2: example pin assignments for various pwm enhanced modes note: to prevent the generation of an incomplete waveform when the pwm is first enabled, the eccp module waits until the start of a new pwm period before generating a pwm signal. ccpr1l ccpr1h (slave) comparator tmr2 comparator pr2 (1) rq s duty cycle registers dc1b<1:0> clear timer2, toggle pwm pin and latch duty cycle note 1: the 8-bit timer tmr2 register is concatenated with the 2-bit internal q clock, or 2 bits of the prescaler to create the 10-bit time base. tris ccp1/p1a tris p1b tris p1c tris p1d output controller p1m<1:0> 2 ccp1m<3:0> 4 pwm1con ccp1/p1a p1b p1c p1d note 1: the tris register value for each pwm output must be configured appropriately. 2: any pin not used by an enhanced pwm mode is available for alternate pin functions. eccp mode p1m<1:0> ccp1/p1a p1b p1c p1d single 00 yes (1) yes (1) yes (1) yes (1) half-bridge 10 yes yes no no full-bridge, forward 01 yes yes yes yes full-bridge, reverse 11 yes yes yes yes note 1: outputs are enabled by pulse steering in single mode. see register 14-4 .
pic18f/lf1xk50 ds41350e-page 122 preliminary ? 2010 microchip technology inc. figure 14-4: example pwm (enhanced mode) output relationships (active-high state) 0 period 00 10 01 11 signal pr2+1 p1m<1:0> p1a modulated p1a modulated p1b modulated p1a active p1b inactive p1c inactive p1d modulated p1a inactive p1b modulated p1c active p1d inactive pulse width (single output) (half-bridge) (full-bridge, forward) (full-bridge, reverse) delay (1) delay (1) relationships: ? period = 4 * t osc * (pr2 + 1) * (tmr2 prescale value) ? pulse width = t osc * (ccpr1l<7:0>:ccp1con<5:4>) * (tmr2 prescale value) ? delay = 4 * t osc * (pwm1con<6:0>) note 1: dead-band delay is programmed using the pwm1con register ( section 14.4.6 ?programmable dead-band delay mode? ).
? 2010 microchip technology inc. preliminary ds41350e-page 123 pic18f/lf1xk50 figure 14-5: example enhanced pwm outp ut relationships (active-low state) 0 period 00 10 01 11 signal pr2+1 p1m<1:0> p1a modulated p1a modulated p1b modulated p1a active p1b inactive p1c inactive p1d modulated p1a inactive p1b modulated p1c active p1d inactive pulse width (single output) (half-bridge) (full-bridge, forward) (full-bridge, reverse) delay (1) delay (1) relationships: ? period = 4 * t osc * (pr2 + 1) * (tmr2 prescale value) ? pulse width = t osc * (ccpr1l<7:0>:ccp1con<5:4>) * (tmr2 prescale value) ? delay = 4 * t osc * (pwm1con<6:0>) note 1: dead-band delay is programmed using the pwm1con register ( section 14.4.6 ?programmable dead-band delay mode? ).
pic18f/lf1xk50 ds41350e-page 124 preliminary ? 2010 microchip technology inc. 14.4.1 half-bridge mode in half-bridge mode, two pins are used as outputs to drive push-pull loads. the pwm output signal is output on the ccp1/p1a pin, while the complementary pwm output signal is output on the p1b pin (see figure 14-6 ). this mode can be used for half-bridge applications, as shown in figure 14-7 , or for full-bridge applications, where four power switches are being modulated with two pwm signals. in half-bridge mode, the programmable dead-band delay can be used to prevent shoot-through current in half-bridge power devices. the value of the pdc<6:0> bits of the pwm1con register sets the number of instruction cycles before the output is driven active. if the value is greater than the duty cycle, the corresponding output remains inactive during the entire cycle. see section 14.4.6 ?programmable dead-band delay mode? for more details of the dead-band delay operations. since the p1a and p1b outputs are multiplexed with the port data latches, the associated tris bits must be cleared to configure p1a and p1b as outputs. figure 14-6: example of half-bridge pwm output figure 14-7: example of half-bridge applications period pulse width td td (1) p1a (2) p1b (2) td = dead-band delay period (1) (1) note 1: at this time, the tmr2 register is equal to the pr2 register. 2: output signals are shown as active-high. p1a p1b fet driver fet driver load + - + - fet driver fet driver v+ load fet driver fet driver p1a p1b standard half-bridge circuit (?push-pull?) half-bridge output driving a full-bridge circuit
? 2010 microchip technology inc. preliminary ds41350e-page 125 pic18f/lf1xk50 14.4.2 full-bridge mode in full-bridge mode, all four pins are used as outputs. an example of full-bridge application is shown in figure 14-8 . in the forward mode, pin ccp1/p1a is driven to its active state, pin p1d is modulated, while p1b and p1c will be driven to their inactive state as shown in figure 14-9 . in the reverse mode, p1c is driven to its active state, pin p1b is modulated, while p1a and p1d will be driven to their inactive state as shown figure 14-9 . p1a, p1b, p1c and p1d outputs are multiplexed with the port data latches. the associated tris bits must be cleared to configure the p1a, p1b, p1c and p1d pins as outputs. figure 14-8: example of full-bridge application p1a p1c fet driver fet driver v+ v- load fet driver fet driver p1b p1d qa qb qd qc
pic18f/lf1xk50 ds41350e-page 126 preliminary ? 2010 microchip technology inc. figure 14-9: example of full-bridge pwm output period pulse width p1a (2) p1b (2) p1c (2) p1d (2) forward mode (1) period pulse width p1a (2) p1c (2) p1d (2) p1b (2) reverse mode (1) (1) (1) note 1: at this time, the tmr2 register is equal to the pr2 register. 2: output signal is shown as active-high.
? 2010 microchip technology inc. preliminary ds41350e-page 127 pic18f/lf1xk50 14.4.2.1 direction change in full-bridge mode in the full-bridge mode, the p1m1 bit in the ccp1con register allows users to control the forward/reverse direction. when the application firmware changes this direction control bit, the module will change to the new direction on the next pwm cycle. a direction change is initiated in software by changing the p1m1 bit of the ccp1con register. the following sequence occurs prior to the end of the current pwm period: ? the modulated outputs (p1b and p1d) are placed in their inactive state. ? the associated unmodulated outputs (p1a and p1c) are switched to drive in the opposite direction. ? pwm modulation resumes at the beginning of the next period. see figure 14-10 for an illustration of this sequence. the full-bridge mode does not provide dead-band delay. as one output is modulated at a time, dead-band delay is generally not required. there is a situation where dead-band delay is required. this situation occurs when both of the following conditions are true: 1. the direction of the pwm output changes when the duty cycle of the output is at or near 100%. 2. the turn off time of the power switch, including the power device and driver circuit, is greater than the turn on time. figure 14-11 shows an example of the pwm direction changing from forward to reverse, at a near 100% duty cycle. in this example, at time t1, the output p1a and p1d become inactive, while output p1c becomes active. since the turn off time of the power devices is longer than the turn on time, a shoot-through current will flow through power devices qc and qd (see figure 14-8 ) for the duration of ?t?. the same phenomenon will occur to power devices qa and qb for pwm direction change from reverse to forward. if changing pwm direction at high duty cycle is required for an application, two possible solutions for eliminating the shoot-through current are: 1. reduce pwm duty cycle for one pwm period before changing directions. 2. use switch drivers that can drive the switches off faster than they can drive them on. other options to prevent shoot-through current may exist. figure 14-10: example of pwm direction change pulse width period (1) signal note 1: the direction bit p1m1 of the ccp1con register is written any time during the pwm cycle. period p1a (active-high) p1b (active-high) p1c (active-high) p1d (active-high) pulse width
pic18f/lf1xk50 ds41350e-page 128 preliminary ? 2010 microchip technology inc. figure 14-11: example of pwm direct ion change at near 100% duty cycle 14.4.3 start-up considerations when any pwm mode is used, the application hardware must use the proper external pull-up and/or pull-down resistors on the pwm output pins. the ccp1m<1:0> bits of the ccp1con register allow the user to choose whether the pwm output signals are active-high or active-low for each pair of pwm output pins (p1a/p1c and p1b/p1d). the pwm output polarities must be selected before the pwm pin output drivers are enabled. changing the polarity configuration while the pwm pin output drivers are enable is not recommended since it may result in damage to the application circuits. the p1a, p1b, p1c and p1d output latches may not be in the proper states when the pwm module is initialized. enabling the pwm pin output drivers at the same time as the enhanced pwm modes may cause damage to the application circuit. the enhanced pwm modes must be enabled in the proper output mode and complete a full pwm cycle before enabling the pwm pin output drivers. the completion of a full pwm cycle is indicated by the tmr2if bit of the pir1 register being set as the second pwm period begins. forward period reverse period p1a t on t off t = t off ? t on p1b p1c p1d external switch d potential shoot-through current note 1: all signals are shown as active-high. 2: t on is the turn on delay of power switch qc and its driver. 3: t off is the turn off delay of pow er switch qd and its driver. external switch c t1 pw pw note: when the microcontroller is released from reset, all of the i/o pins are in the high-impedance state. the external cir- cuits must keep the power switch devices in the off state until the microcontroller drives the i/o pins with the proper signal levels or activates the pwm output(s).
? 2010 microchip technology inc. preliminary ds41350e-page 129 pic18f/lf1xk50 14.4.4 enhanced pwm auto-shutdown mode the pwm mode supports an auto-shutdown mode that will disable the pwm outputs when an external shutdown event occurs. auto-shutdown mode places the pwm output pins into a predetermined state. this mode is used to help prevent the pwm from damaging the application. the auto-shutdown sources are selected using the eccpas<2:0> bits of the eccpas register. a shutdown event may be generated by: ?a logic ? 0 ? on the int0 pin ?a logic ? 1 ? on a comparator (cx) output a shutdown condition is indicated by the eccpase (auto-shutdown event status) bit of the eccpas register. if the bit is a ? 0 ?, the pwm pins are operating normally. if the bit is a ? 1 ?, the pwm outputs are in the shutdown state. when a shutdown event occurs, two things happen: the eccpase bit is set to ? 1 ?. the eccpase will remain set until cleared in firmware or an auto-restart occurs (see section 14.4.5 ?auto-restart mode? ). the enabled pwm pins are asynchronously placed in their shutdown states. the pwm output pins are grouped into pairs [p1a/p1c] and [p1b/p1d]. the state of each pin pair is determined by the pssac and pssbd bits of the eccpas register. each pin pair may be placed into one of three states: ? drive logic ? 1 ? ? drive logic ? 0 ? ? tri-state (high-impedance) register 14-2: eccp1as: enhanced cap ture/compare/pwm auto-shutdown control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 eccpase eccpas2 eccpas1 eccpas0 pssac1 pssac0 pssbd1 pssbd0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 eccpase: eccp auto-shutdown event status bit 1 = a shutdown event has occurred; eccp outputs are in shutdown state 0 = eccp outputs are operating bit 6-4 eccpas<2:0>: eccp auto-shutdown source select bits 000 = auto-shutdown is disabled 001 = comparator c1out output is high 010 = comparator c2out output is high 011 = either comparator c1out or c2out is high 100 =v il on int0 pin 101 =v il on int0 pin or comparator c1out output is high 110 =v il on int0 pin or comparator c2out output is high 111 =v il on int0 pin or comparator c1out or comparator c2out is high bit 3-2 pssacn: pins p1a and p1c shutdown state control bits 00 = drive pins p1a and p1c to ? 0 ? 01 = drive pins p1a and p1c to ? 1 ? 1x = pins p1a and p1c tri-state bit 1-0 pssbdn: pins p1b and p1d shutdown state control bits 00 = drive pins p1b and p1d to ? 0 ? 01 = drive pins p1b and p1d to ? 1 ? 1x = pins p1b and p1d tri-state
pic18f/lf1xk50 ds41350e-page 130 preliminary ? 2010 microchip technology inc. figure 14-12: pwm auto-shutdown wi th firmware restart (prsen = 0 ) note 1: the auto-shutdown condition is a level-based signal, not an edge-based signal. as long as the level is present, the auto-shutdown will persist. 2: writing to the eccpase bit is disabled while an auto-shutdown condition persists. 3: once the auto-shutdown condition has been removed and the pwm restarted (either through firmware or auto-restart) the pwm signal will always restart at the beginning of the next pwm period. 4: prior to an auto-shutdown event caused by a comparator output or int pin event, a software shutdown can be triggered in firmware by setting the ccpxase bit to a ? 1 ?. the auto-restart feature tracks the active status of a shutdown caused by a comparator output or int pin event only so, if it is enabled at this time. it will imme- diately clear this bit and restart the eccp module at the beginning of the next pwm period. shutdown pwm eccpase bit activity event shutdown event occurs shutdown event clears pwm resumes normal pwm start of pwm period eccpase cleared by firmware pwm period
? 2010 microchip technology inc. preliminary ds41350e-page 131 pic18f/lf1xk50 14.4.5 auto-restart mode the enhanced pwm can be configured to automati- cally restart the pwm signal once the auto-shutdown condition has been removed. auto-restart is enabled by setting the prsen bit in the pwm1con register. if auto-restart is enabled, the eccpase bit will remain set as long as the auto-shutdown condition is active. when the auto-shutdown condition is removed, the eccpase bit will be cleared via hardware and normal operation will resume. figure 14-13: pwm auto-shutdown wi th auto-restart enabled (prsen = 1 ) shutdown pwm eccpase bit activity event shutdown event occurs shutdown event clears pwm resumes normal pwm start of pwm period pwm period
pic18f/lf1xk50 ds41350e-page 132 preliminary ? 2010 microchip technology inc. 14.4.6 programmable dead-band delay mode in half-bridge applications where all power switches are modulated at the pwm frequency, the power switches normally require more time to turn off than to turn on. if both the upper and lower power switches are switched at the same time (one turned on, and the other turned off), both switches may be on for a short period of time until one switch completely turns off. during this brief interval, a very high current (shoot-through current) will flow through both power switches, shorting the bridge supply. to avoid this potentially destructive shoot-through current from flowing during switching, turning on either of the power switches is normally delayed to allow the other switch to completely turn off. in half-bridge mode, a digitally programmable dead-band delay is available to avoid shoot-through current from destroying the bridge power switches. the delay occurs at the signal transition from the non-active state to the active state. see figure 14-14 for illustration. the lower seven bits of the associated pwm1con register ( register 14-3 ) sets the delay period in terms of microcontroller instruction cycles (t cy or 4 t osc ). figure 14-14: example of half-bridge pwm output figure 14-15: example of half-bridge applications period pulse width td td (1) p1a (2) p1b (2) td = dead-band delay period (1) (1) note 1: at this time, the tmr2 register is equal to the pr2 register. 2: output signals are shown as active-high. p1a p1b fet driver fet driver v+ v- load + v - + v - standard half-bridge circuit (?push-pull?)
? 2010 microchip technology inc. preliminary ds41350e-page 133 pic18f/lf1xk50 register 14-3: pwm1con: enhanced pwm control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 prsen pdc6 pdc5 pdc4 pdc3 pdc2 pdc1 pdc0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 prsen: pwm restart enable bit 1 = upon auto-shutdown, the eccpase bit clears automatically once the shutdown event goes away; the pwm restarts automatically 0 = upon auto-shutdown, eccpase must be cleared by software to restart the pwm bit 6-0 pdc<6:0>: pwm delay count bits pdcn = number of f osc /4 (4 * t osc ) cycles between the scheduled time when a pwm signal should transition active and the actual time it transitions active
pic18f/lf1xk50 ds41350e-page 134 preliminary ? 2010 microchip technology inc. 14.4.7 pulse steering mode in single output mode, pulse steering allows any of the pwm pins to be the modulated signal. additionally, the same pwm signal can be simultaneously available on multiple pins. once the single output mode is selected (ccp1m<3:2> = 11 and p1m<1:0> = 00 of the ccp1con register), the user firmware can bring out the same pwm signal to one, two, three or four output pins by setting the appropriate str bits of the pstrcon register, as shown in tab l e 1 4- 2 . while the pwm steering mode is active, ccp1m<1:0> bits of the ccp1con register select the pwm output polarity for the p1 pins. the pwm auto-shutdown operation also applies to pwm steering mode as described in section 14.4.4 ?enhanced pwm auto-shutdown mode? . an auto-shutdown event will only affect pins that have pwm outputs enabled. note: the associated tris bits must be set to output (? 0 ?) to enable the pin output driver in order to see the pwm signal on the pin. register 14-4: pstrcon: pul se steering control register (1) u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-1 ? ? ? strsync strd strc strb stra bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-5 unimplemented: read as ? 0 ? bit 4 strsync: steering sync bit 1 = output steering update occurs on next pwm period 0 = output steering update occurs at the beginning of the instruction cycle boundary bit 3 strd: steering enable bit d 1 = p1d pin has the pwm waveform with polarity control from ccp1m<1:0> 0 = p1d pin is assigned to port pin bit 2 strc: steering enable bit c 1 = p1c pin has the pwm waveform with polarity control from ccp1m<1:0> 0 = p1c pin is assigned to port pin bit 1 strb: steering enable bit b 1 = p1b pin has the pwm waveform with polarity control from ccp1m<1:0> 0 = p1b pin is assigned to port pin bit 0 stra: steering enable bit a 1 = p1a pin has the pwm waveform with polarity control from ccp1m<1:0> 0 = p1a pin is assigned to port pin note 1: the pwm steering mode is available only when the ccp1con register bits ccp1m<3:2> = 11 and p1m<1:0> = 00 .
? 2010 microchip technology inc. preliminary ds41350e-page 135 pic18f/lf1xk50 figure 14-16: simplified steering block diagram 1 0 tris p1a pin port data p1a signal stra 1 0 tris p1b pin port data strb 1 0 tris p1c pin port data strc 1 0 tris p1d pin port data strd note 1: port outputs are configured as shown when the ccp1con register bits p1m<1:0> = 00 and ccp1m<3:2> = 11. 2: single pwm output requires setting at least one of the strx bits. ccp1m1 ccp1m0 ccp1m1 ccp1m0
pic18f/lf1xk50 ds41350e-page 136 preliminary ? 2010 microchip technology inc. 14.4.7.1 steering synchronization the strsync bit of the pstrcon register gives the user two selections of when the steering event will happen. when the strsync bit is ? 0 ?, the steering event will happen at the end of the instruction that writes to the pstrcon register. in this case, the output signal at the p1 pins may be an incomplete pwm waveform. this operation is useful when the user firmware needs to immediately remove a pwm signal from the pin. when the strsync bit is ? 1 ?, the effective steering update will happen at the beginning of the next pwm period. in this case, steering on/off the pwm output will always produce a complete pwm waveform. figures 14-17 and 14-18 illustrate the timing diagrams of the pwm steering depending on the strsync setting. figure 14-17: example of steering ev ent at end of instruction (strsync = 0) figure 14-18: example of steering event at beginning of instruction (strsync = 1) pwm p1n = pwm strn p1 port data pwm period port data pwm port data p1n = pwm strn p1 port data
? 2010 microchip technology inc. preliminary ds41350e-page 137 pic18f/lf1xk50 14.4.8 operation in power-managed modes in sleep mode, all clock sources are disabled. timer2 will not increment and the state of the module will not change. if the eccp pin is driving a value, it will con- tinue to drive that value. when the device wakes up, it will continue from this state. if two-speed start-ups are enabled, the initial start-up frequency from hfintosc and the postscaler may not be stable immediately. in pri_idle mode, the primary clock will continue to clock the eccp module without change. in all other power-managed modes, the selected power-managed mode clock will clock timer2. other power-managed mode clocks will most likely be different than the primary clock frequency. 14.4.8.1 operation with fail-safe clock monitor if the fail-safe clock monitor is enabled, a clock failure will force the device into the rc_run power-managed mode and the oscfif bit of the pir2 register will be set. the eccp will then be clocked from the internal oscillator clock source, which may have a different clock frequency than the primary clock. see the previous section for additional details. 14.4.9 effects of a reset both power-on reset and subsequent resets will force all ports to input mode and the ccp registers to their reset states. this forces the enhanced ccp module to reset to a state compatible with the standard ccp module.
pic18f/lf1xk50 ds41350e-page 138 preliminary ? 2010 microchip technology inc. table 14-3: registers associated with eccp1 module and timer1 to timer3 name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 285 rcon ipen sboren ? ri to pd por bor 284 pir1 ? adif rcif txif sspif ccp1if tmr2if tmr1if 288 pie1 ? adie rcie txie sspie ccp1ie tmr2ie tmr1ie 288 ipr1 ? adip rcip txip sspip ccp1ip tmr2ip tmr1ip 288 pir2 oscfif c1if c2if eeif bclif usbif tmr3if ?288 pie2 oscfie c1ie c2ie eeie bclie usbie tmr3ie ?288 ipr2 oscfip c1ip c2ip eeip bclip usbip tmr3ip ?288 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 288 tmr1l timer1 register, low byte 286 tmr1h timer1 register, high byte 286 t1con rd16 t1run t1ckps1 t1ckps0 t1oscen t1sync tmr1cs tmr1on 286 tmr2 timer2 register 286 t2con ? t2outps3 t2outps2 t2outps1 t2outps0 tmr2on t2ckps1 t2ckps0 286 pr2 timer2 period register 286 tmr3l timer3 register, low byte 287 tmr3h timer3 register, high byte 287 t3con rd16 ? t3ckps1 t3ckps0 t3ccp1 t3sync tmr3cs tmr3on 287 ccpr1l capture/compare/pwm register 1, low byte 287 ccpr1h capture/compare/pwm register 1, high byte 287 ccp1con p1m1 p1m0 dc1b1 dc1b0 ccp1m3 ccp1m2 ccp1m1 ccp1m0 287 eccp1as eccpase eccpas2 eccpas1 eccpas0 pssac1 pssac0 pssbd1 pssbd0 287 pwm1con prsen pdc6 pdc5 pdc4 pdc3 pdc2 pdc1 pdc0 287 legend: ? = unimplemented, read as ? 0 ?. shaded cells are not used during eccp operation.
? 2010 microchip technology inc. preliminary ds41350e-page 139 pic18f/lf1xk50 15.0 master synchronous serial port (mssp) module 15.1 master ssp (mssp) module overview the master synchronous serial port (mssp) module is a serial interface, useful for communicating with other peripheral or microcontroller devices. these peripheral devices may be serial eeproms, shift registers, dis- play drivers, a/d converters, etc. the mssp module can operate in one of two modes: ? serial peripheral interface (spi) ? inter-integrated circuit (i 2 c?) - full master mode - slave mode (with general address call) the i 2 c interface supports the following modes in hardware: ?master mode ? multi-master mode ? slave mode 15.2 spi mode the spi mode allows 8 bits of data to be synchronously transmitted and received simultaneously. all four modes of spi are supported. to accomplish communication, typically three pins are used: ? serial data out ? sdo ? serial data in ? sdi ? serial clock ? sck additionally, a fourth pin may be used when in a slave mode of operation: ? slave select ? ss figure 15-1 shows the block diagram of the mssp module when operating in spi mode. figure 15-1: mssp block diagram (spi mode) ( ) read write internal data bus sspsr reg sspm<3:0> bit 0 shift clock ss control enable edge select clock select tmr2 output t osc prescaler 4, 16, 64 2 edge select 2 4 tris bit sdo sspbuf reg sdi/sda ss sck/scl
pic18f/lf1xk50 ds41350e-page 140 preliminary ? 2010 microchip technology inc. 15.2.1 registers the mssp module has four registers for spi mode operation. these are: ? sspcon1 ? control register ? sspstat ? status register ? sspbuf ? serial receive/transmit buffer ? sspsr ? shift register (not directly accessible) sspcon1 and sspstat are the control and status registers in spi mode operation. the sspcon1 regis- ter is readable and writable. the lower 6 bits of the sspstat are read-only. the upper two bits of the sspstat are read/write. sspsr is the shift register used for shifting data in and out. sspbuf provides indirect access to the sspsr register. sspbuf is the buffer register to which data bytes are written, and from which data bytes are read. in receive operations, sspsr and sspbuf together create a double-buffered receiver. when sspsr receives a complete byte, it is transferred to sspbuf and the sspif interrupt is set. during transmission, the sspbuf is not double-buffered. a write to sspbuf will write to both sspbuf and sspsr. register 15-1: sspstat: mssp status register (spi mode) r/w-0 r/w-0 r-0 r-0 r-0 r-0 r-0 r-0 smp cke d/a psr/w ua bf bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 smp: sample bit spi master mode: 1 = input data sampled at end of data output time 0 = input data sampled at middle of data output time spi slave mode: smp must be cleared when spi is used in slave mode. bit 6 cke: spi clock select bit (1) 1 = transmit occurs on transition from active to idle clock state 0 = transmit occurs on transition from idle to active clock state bit 5 d/a : data/address bit used in i 2 c mode only. bit 4 p: stop bit used in i 2 c mode only. this bit is cleared when the mssp module is disabled, sspen is cleared. bit 3 s: start bit used in i 2 c mode only. bit 2 r/w : read/write information bit used in i 2 c mode only. bit 1 ua: update address bit used in i 2 c mode only. bit 0 bf: buffer full status bit (receive mode only) 1 = receive complete, sspbuf is full 0 = receive not complete, sspbuf is empty note 1: polarity of clock state is set by the ckp bit of the sspcon1 register.
? 2010 microchip technology inc. preliminary ds41350e-page 141 pic18f/lf1xk50 register 15-2: sspcon1: mssp co ntrol 1 register (spi mode) r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 wcol: write collision detect bit (transmit mode only) 1 = the sspbuf register is written while it is still transmitting the previous word (must be cleared by software) 0 = no collision bit 6 sspov: receive overflow indicator bit (1) spi slave mode: 1 = a new byte is received while the sspbuf register is still holding the previous data. in case of over- flow, the data in sspsr is lost. overflow can only occur in slave mode. the user must read the sspbuf, even if only transmitting data, to avoid setting overflow (must be cleared by software). 0 = no overflow bit 5 sspen: synchronous serial port enable bit (2) 1 = enables serial port and configures sck, sdo, sdi and ss as serial port pins 0 = disables serial port and configures these pins as i/o port pins bit 4 ckp: clock polarity select bit 1 = idle state for clock is a high level 0 = idle state for clock is a low level bit 3-0 sspm<3:0>: synchronous serial port mode select bits (3) 0101 = spi slave mode, clock = sck pin, ss pin control disabled, ss can be used as i/o pin 0100 = spi slave mode, clock = sck pin, ss pin control enabled 0011 = spi master mode, clock = tmr2 output/2 0010 = spi master mode, clock = f osc /64 0001 = spi master mode, clock = f osc /16 0000 = spi master mode, clock = f osc /4 note 1: in master mode, the overflow bit is not set since each new reception (and transmission) is initiated by writing to the sspbuf register. 2: when enabled, these pins must be properly configured as input or output. 3: bit combinations not specifically listed here are either reserved or implemented in i 2 c mode only.
pic18f/lf1xk50 ds41350e-page 142 preliminary ? 2010 microchip technology inc. 15.2.2 operation when initializing the spi, several options need to be specified. this is done by programming the appropriate control bits (sspcon1<5:0> and sspstat<7:6>). these control bits allow the following to be specified: ? master mode (sck is the clock output) ? slave mode (sck is the clock input) ? clock polarity (idle state of sck) ? data input sample phase (middle or end of data output time) ? clock edge (output data on rising/falling edge of sck) ? clock rate (master mode only) ? slave select mode (slave mode only) the mssp consists of a transmit/receive shift register (sspsr) and a buffer register (sspbuf). the sspsr shifts the data in and out of the device, msb first. the sspbuf holds the data that was written to the sspsr until the received data is ready. once the 8 bits of data have been received, that byte is moved to the sspbuf register. then, the buffer full detect bit, bf of the sspstat register, and the interrupt flag bit, sspif, are set. this double-buffering of the received data (sspbuf) allows the next byte to start reception before reading the data that was just received. any write to the sspbuf register during transmission/reception of data will be ignored and the write collision detect bit wcol of the sspcon1 register, will be set. user software must clear the wcol bit to allow the following write(s) to the sspbuf register to complete successfully. when the application software is expecting to receive valid data, the sspbuf should be read before the next byte of data to transfer is written to the sspbuf. the buffer full bit, bf of the sspstat register, indicates when sspbuf has been loaded with the received data (transmission is complete). when the sspbuf is read, the bf bit is cleared. this data may be irrelevant if the spi is only a transmitter. generally, the mssp interrupt is used to determine when the transmission/reception has completed. if the interrupt method is not going to be used, then software polling can be done to ensure that a write collision does not occur. example 15-1 shows the loading of the sspbuf (sspsr) for data transmission. the sspsr is not directly readable or writable and can only be accessed by addressing the sspbuf register. additionally, the mssp status register (sspstat) indicates the various status conditions. example 15-1: loading the sspbuf (sspsr) register loop btfss sspstat, bf ;has data been received (transmit complete)? bra loop ;no movf sspbuf, w ;wreg reg = contents of sspbuf movwf rxdata ;save in user ram, if data is meaningful movf txdata, w ;w reg = contents of txdata movwf sspbuf ;new data to xmit
? 2010 microchip technology inc. preliminary ds41350e-page 143 pic18f/lf1xk50 15.2.3 enabling spi i/o to enable the serial port, ssp enable bit, sspen of the sspcon1 register, must be set. to reset or reconfig- ure spi mode, clear the sspen bit, reinitialize the sspcon registers and then set the sspen bit. this configures the sdi, sdo, sck and ss pins as serial port pins. for the pins to behave as the serial port func- tion, some must have their data direction bits (in the tris register) appropriately programmed as follows: ? sdi is automatically controlled by the spi module ? sdo must have corresponding tris bit cleared ? sck (master mode) must have corresponding tris bit cleared ? sck (slave mode) must have corresponding tris bit set ?ss must have corresponding tris bit set any serial port function that is not desired may be overridden by programming the corresponding data direction (tris) register to the opposite value. 15.2.4 typical connection figure 15-2 shows a typical connection between two microcontrollers. the master controller (processor 1) initiates the data transfer by sending the sck signal. data is shifted out of both shift registers on their pro- grammed clock edge and latched on the opposite edge of the clock. both processors should be programmed to the same clock polarity (ckp), then both controllers would send and receive data at the same time. whether the data is meaningful (or dummy data) depends on the application software. this leads to three scenarios for data transmission: ? master sends data ? ? ? slave sends dummy data ? master sends data ? ? ? slave sends data ? master sends dummy data ? ? ? slave sends data figure 15-2: typical spi mast er/slave connection serial input buffer (sspbuf) shift register (sspsr) msb lsb sdo sdi processor 1 sck spi master sspm<3:0> = 00xx serial input buffer (sspbuf) shift register (sspsr) lsb msb sdi sdo processor 2 sck spi slave sspm<3:0> = 010x serial clock ss slave select general i/o (optional)
pic18f/lf1xk50 ds41350e-page 144 preliminary ? 2010 microchip technology inc. 15.2.5 master mode the master can initiate the data transfer at any time because it controls the sck. the master determines when the slave (processor 2, figure 15-2 ) is to broadcast data by the software protocol. in master mode, the data is transmitted/received as soon as the sspbuf register is written to. if the spi is only going to receive, the sdo output could be dis- abled (programmed as an input). the sspsr register will continue to shift in the signal present on the sdi pin at the programmed clock rate. as each byte is received, it will be loaded into the sspbuf register as if a normal received byte (interrupts and status bits appropriately set). the clock polarity is selected by appropriately programming the ckp bit of the sspcon1 register. this then, would give waveforms for spi communication as shown in figure 15-3 , figure 15-5 and figure 15-6 , where the msb is transmitted first. in master mode, the spi clock rate (bit rate) is user programmable to be one of the following: ?f osc /4 (or t cy ) ?f osc /16 (or 4 ? t cy ) ?f osc /64 (or 16 ? t cy ) ? timer2 output/2 this allows a maximum data rate (at 64 mhz) of 16.00 mbps. figure 15-3 shows the waveforms for master mode. when the cke bit is set, the sdo data is valid before there is a clock edge on sck. the change of the input sample is shown based on the state of the smp bit. the time when the sspbuf is loaded with the received data is shown. figure 15-3: spi mode waveform (master mode) sck (ckp = 0 sck (ckp = 1 sck (ckp = 0 sck (ckp = 1 4 clock modes input sample input sample sdi bit 7 bit 0 sdo bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 bit 7 sdi sspif (smp = 1 ) (smp = 0 ) (smp = 1 ) cke = 1 ) cke = 0 ) cke = 1 ) cke = 0 ) (smp = 0 ) write to sspbuf sspsr to sspbuf sdo bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 (cke = 0 ) (cke = 1 ) bit 0
? 2010 microchip technology inc. preliminary ds41350e-page 145 pic18f/lf1xk50 15.2.6 slave mode in slave mode, the data is transmitted and received as external clock pulses appear on sck. when the last bit is latched, the sspif interrupt flag bit is set. before enabling the module in spi slave mode, the clock line must match the proper idle state. the clock line can be observed by reading the sck pin. the idle state is determined by the ckp bit of the sspcon1 register. while in slave mode, the external clock is supplied by the external clock source on the sck pin. this external clock must meet the minimum high and low times as specified in the electrical specifications. while in sleep mode, the slave can transmit/receive data. when a byte is received, the device will wake-up from sleep. 15.2.7 slave select synchronization the ss pin allows a synchronous slave mode. the spi must be in slave mode with ss pin control enabled (sspcon1<3:0> = 0100 ). when the ss pin is low, transmission and reception are enabled and the sdo pin is driven. when the ss pin goes high, the sdo pin is no longer driven, even if in the middle of a transmitted byte and becomes a floating output. external pull-up/pull-down resistors may be desirable depend- ing on the application. when the spi module resets, the bit counter is forced to ? 0 ?. this can be done by either forcing the ss pin to a high level or clearing the sspen bit. figure 15-4: slave synchronization waveform note 1: when the spi is in slave mode with ss pin control enabled (sspcon<3:0> = 0100 ), the spi module will reset if the ss pin is set to v dd . 2: when the spi is used in slave mode with cke set the ss pin control must also be enabled. sck (ckp = 1 sck (ckp = 0 input sample sdi bit 7 sdo bit 7 bit 6 bit 7 sspif interrupt (smp = 0 ) cke = 0 ) cke = 0 ) (smp = 0 ) write to sspbuf sspsr to sspbuf ss flag bit 0 bit 7 bit 0
pic18f/lf1xk50 ds41350e-page 146 preliminary ? 2010 microchip technology inc. figure 15-5: spi mode waveform (slave mode with cke = 0 ) figure 15-6: spi mode waveform (slave mode with cke = 1 ) sck (ckp = 1 sck (ckp = 0 input sample sdi bit 7 sdo bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 sspif interrupt (smp = 0 ) cke = 0 ) cke = 0 ) (smp = 0 ) write to sspbuf sspsr to sspbuf ss flag optional bit 0 sck (ckp = 1 sck (ckp = 0 input sample sdi bit 7 bit 0 sdo bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 sspif interrupt (smp = 0 ) cke = 1 ) cke = 1 ) (smp = 0 ) write to sspbuf sspsr to sspbuf ss flag not optional
? 2010 microchip technology inc. preliminary ds41350e-page 147 pic18f/lf1xk50 15.2.8 operation in power-managed modes in spi master mode, module clocks may be operating at a different speed than when in full power mode; in the case of the sleep mode, all clocks are halted. in all idle modes, a clock is provided to the peripherals. that clock could be from the primary clock source, the secondary clock (timer1 oscillator at 32.768 khz) or the intosc source. see section 19.0 ?power-man- aged modes? for additional information. in most cases, the speed that the master clocks spi data is not important; however, this should be evaluated for each system. when mssp interrupts are enabled, after the master completes sending data, an mssp interrupt will wake the controller: ? from sleep, in slave mode ? from idle, in slave or master mode if an exit from sleep or idle mode is not desired, mssp interrupts should be disabled. in spi master mode, when the sleep mode is selected, all module clocks are halted and the transmis- sion/reception will remain in that state until the devices wakes. after the device returns to run mode, the mod- ule will resume transmitting and receiving data. in spi slave mode, the spi transmit/receive shift register operates asynchronously to the device. this allows the device to be placed in any power-managed mode and data to be shifted into the spi transmit/receive shift register. when all 8 bits have been received, the mssp interrupt flag bit will be set and if enabled, will wake the device. 15.2.9 effects of a reset a reset disables the mssp module and terminates the current transfer. 15.2.10 bus mode compatibility table 15-1 shows the compatibility between the standard spi modes and the states of the ckp and cke control bits. table 15-1: spi bus modes there is also an smp bit which controls when the data is sampled. table 15-2: registers associat ed with spi operation standard spi mode terminology control bits state ckp cke 0, 0 0 1 0, 1 0 0 1, 0 1 1 1, 1 1 0 name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 285 pir1 ? adif rcif txif sspif ccp1if tmr2if tmr1if 288 pie1 ? adie rcie txie sspie ccp1ie tmr2ie tmr1ie 288 ipr1 ? adip rcip txip sspip ccp1ip tmr2ip tmr1ip 288 trisb trisb7 trisb6 trisb5 trisb4 ? ? ? ? 288 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 288 sspbuf ssp receive buffer/transmit register 286 sspcon1 wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 286 sspstat smp cke d/a p s r/w ua bf 286 legend: shaded cells are not used by the mssp in spi mode.
pic18f/lf1xk50 ds41350e-page 148 preliminary ? 2010 microchip technology inc. 15.3 i 2 c mode the mssp module in i 2 c mode fully implements all master and slave functions (including general call support) and provides interrupts on start and stop bits in hardware to determine a free bus (multi-master function). the mssp module implements the standard mode specifications as well as 7-bit and 10-bit addressing. two pins are used for data transfer: ? serial clock ? scl ? serial data ? sda figure 15-7: mssp block diagram (i 2 c? mode) 15.3.1 registers the mssp module has seven registers for i 2 c operation. these are: ? mssp control register 1 (sspcon1) ? mssp control register 2 (sspcon2) ? mssp status register (sspstat) ? serial receive/transmit buffer register (sspbuf) ? mssp shift register (sspsr) ? not directly accessible ? mssp address register (sspadd) ? mssp address mask (sspmsk) sspcon1, sspcon2 and sspstat are the control and status registers in i 2 c mode operation. the sspcon1 and sspcon2 registers are readable and writable. the lower 6 bits of the sspstat are read-only. the upper two bits of the sspstat are read/write. sspsr is the shift register used for shifting data in or out. sspbuf is the buffer register to which data bytes are written to or read from. when the mssp is configured in master mode, the sspadd register acts as the baud rate generator reload value. when the mssp is configured for i 2 c slave mode the sspadd register holds the slave device address. the mssp can be configured to respond to a range of addresses by qualifying selected bits of the address register with the sspmsk register. in receive operations, sspsr and sspbuf together create a double-buffered receiver. when sspsr receives a complete byte, it is transferred to sspbuf and the sspif interrupt is set. during transmission, the sspbuf is not double-buffered. a write to sspbuf will write to both sspbuf and sspsr. note: the user must configure these pins as inputs with the corresponding tris bits. read write sspsr reg match detect sspadd reg start and stop bit detect sspbuf reg internal data bus addr match set, reset s, p bits (sspstat reg) sck/scl sdi/sda shift clock msb lsb sspmsk reg
? 2010 microchip technology inc. preliminary ds41350e-page 149 pic18f/lf1xk50 register 15-3: sspstat: mssp status register (i 2 c mode) r/w-0 r/w-0 r-0 r-0 r-0 r-0 r-0 r-0 smp cke d/a p (1) s (1) r/w (2, 3) ua bf bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 smp: slew rate control bit in master or slave mode: 1 = slew rate control disabled for standard speed mode (100 khz and 1 mhz) 0 = slew rate control enabled for high-speed mode (400 khz) bit 6 cke: smbus select bit in master or slave mode: 1 = enable smbus specific inputs 0 = disable smbus specific inputs bit 5 d/a : data/address bit in master mode: reserved. in slave mode: 1 = indicates that the last byte received or transmitted was data 0 = indicates that the last byte received was an address bit 4 p: stop bit (1) 1 = indicates that a stop bit has been detected last 0 = stop bit was not detected last bit 3 s: start bit (1) 1 = indicates that a start bit has been detected last 0 = start bit was not detected last bit 2 r/w : read/write information bit (i 2 c mode only) (2, 3) in slave mode: 1 = read 0 = write in master mode: 1 = transmit is in progress 0 = transmit is not in progress bit 1 ua: update address bit (10-bit slave mode only) 1 = indicates that the user needs to update the address in the sspadd register 0 = address does not need to be updated bit 0 bf: buffer full status bit in transmit mode: 1 = sspbuf is full 0 = sspbuf is empty in receive mode: 1 = sspbuf is full (does not include the ack and stop bits) 0 = sspbuf is empty (does not include the ack and stop bits) note 1: this bit is cleared on reset and when sspen is cleared. 2: this bit holds the r/w bit information following the last address match. this bit is only valid from the address match to the next start bit, stop bit or not ack bit. 3: oring this bit with sen, rsen, pen, rcen or acken will indicate if the master mode is active.
pic18f/lf1xk50 ds41350e-page 150 preliminary ? 2010 microchip technology inc. register 15-4: sspcon1: ms sp control 1 register (i 2 c mode) r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 wcol: write collision detect bit in master transmit mode: 1 = a write to the sspbuf register was attempted while the i 2 c conditions were not valid for a trans- mission to be started (must be cleared by software) 0 = no collision in slave transmit mode: 1 = the sspbuf register is written while it is still transmitting the previous word (must be cleared by software) 0 = no collision in receive mode (master or slave modes): this is a ?don?t care? bit. bit 6 sspov: receive overflow indicator bit in receive mode: 1 = a byte is received while the sspbuf register is still holding the previous byte (must be cleared by software) 0 = no overflow in transmit mode: this is a ?don?t care? bit in transmit mode. bit 5 sspen: synchronous serial port enable bit 1 = enables the serial port and configures the sda and scl pins as the serial port pins 0 = disables serial port and configures these pins as i/o port pins when enabled, the sda and scl pins must be properly configured as inputs. bit 4 ckp: sck release control bit in slave mode: 1 = release clock 0 = holds clock low (clock stretch), used to ensure data setup time in master mode: unused in this mode. bit 3-0 sspm<3:0>: synchronous serial port mode select bits 1111 = i 2 c slave mode, 10-bit address with start and stop bit interrupts enabled 1110 = i 2 c slave mode, 7-bit address with start and stop bit interrupts enabled 1011 = i 2 c firmware controlled master mode (slave idle) 1000 = i 2 c master mode, clock = f osc /(4 * (sspadd + 1)) 0111 = i 2 c slave mode, 10-bit address 0110 = i 2 c slave mode, 7-bit address bit combinations not specifically listed here are either reserved or implemented in spi mode only.
? 2010 microchip technology inc. preliminary ds41350e-page 151 pic18f/lf1xk50 register 15-5: sspcon2: ms sp control register (i 2 c mode) r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 gcen ackstat ackdt (2) acken (1) rcen (1) pen (1) rsen (1) sen (1) bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 gcen: general call enable bit (slave mode only) 1 = generate interrupt when a general call address 0x00 or 00h is received in the sspsr 0 = general call address disabled bit 6 ackstat: acknowledge status bit (master transmit mode only) 1 = acknowledge was not received from slave 0 = acknowledge was received from slave bit 5 ackdt: acknowledge data bit (master receive mode only) (2) 1 = not acknowledge 0 = acknowledge bit 4 acken: acknowledge sequence enable bit (master receive mode only) (1) 1 = initiate acknowledge sequence on sda and scl pins and transmit ackdt data bit. automatically cleared by hardware. 0 = acknowledge sequence idle bit 3 rcen: receive enable bit (master mode only) (1) 1 = enables receive mode for i 2 c 0 = receive idle bit 2 pen: stop condition enable bit (master mode only) (1) 1 = initiate stop condition on sda and scl pins. automatically cleared by hardware. 0 = stop condition idle bit 1 rsen: repeated start condition enable bit (master mode only) (1) 1 = initiate repeated start condition on sda and scl pins. automatically cleared by hardware. 0 = repeated start condition idle bit 0 sen: start condition enable/stretch enable bit (1) in master mode: 1 = initiate start condition on sda and scl pins. automatically cleared by hardware. 0 = start condition idle in slave mode: 1 = clock stretching is enabled for both slave transmit and slave receive (stretch enabled) 0 = clock stretching is disabled note 1: for bits acken, rcen, pen, rsen, sen: if the i 2 c module is not in the idle mode, these bits may not be set (no spooling) and the sspbuf may not be written (or writes to the sspbuf are disabled). 2: value that will be transmitted when the user initiates an acknowledge sequence at the end of a receive.
pic18f/lf1xk50 ds41350e-page 152 preliminary ? 2010 microchip technology inc. 15.3.2 operation the mssp module functions are enabled by setting sspen bit of the sspcon1 register. the sspcon1 register allows control of the i 2 c operation. four mode selection bits of the sspcon1 register allow one of the following i 2 c modes to be selected: ?i 2 c master mode, clock = (f osc /(4*(sspadd + 1)) ?i 2 c slave mode (7-bit address) ?i 2 c slave mode (10-bit address) ?i 2 c slave mode (7-bit address) with start and stop bit interrupts enabled ?i 2 c slave mode (10-bit address) with start and stop bit interrupts enabled ?i 2 c firmware controlled master mode, slave is idle selection of any i 2 c mode with the sspen bit set, forces the scl and sda pins to be open-drain, provided these pins are programmed to inputs by setting the appropriate tris bits 15.3.3 slave mode in slave mode, the scl and sda pins must be config- ured as inputs. the mssp module will override the input state with the output data when required (slave-transmitter). the i 2 c slave mode hardware will always generate an interrupt on an address match. through the mode select bits, the user can also choose to interrupt on start and stop bits when an address is matched, or the data transfer after an address match is received, the hardware automatically will generate the acknowledge (ack ) pulse and load the sspbuf register with the received value currently in the sspsr register. any combination of the following conditions will cause the mssp module not to give this ack pulse: ? the buffer full bit, bf bit of the sspstat regis- ter, is set before the transfer is received. ? the overflow bit, sspov bit of the sspcon1 register, is set before the transfer is received. in this case, the sspsr register value is not loaded into the sspbuf, but bit sspif of the pir1 register is set. the bf bit is cleared by reading the sspbuf register, while bit sspov is cleared through software. the scl clock input must have a minimum high and low for proper operation. the high and low times of the i 2 c specification, as well as the requirement of the mssp module, are shown in section 27.0 ?electrical specifications? . 15.3.3.1 addressing once the mssp module has been enabled, it waits for a start condition to occur. following the start condition, the 8 bits are shifted into the sspsr register. all incoming bits are sampled with the rising edge of the clock (scl) line. the value of register sspsr<7:1> is compared to the value of the sspadd register. the address is compared on the falling edge of the eighth clock (scl) pulse. if the addresses match and the bf and sspov bits are clear, the following events occur: 1. the sspsr register value is loaded into the sspbuf register. 2. the buffer full bit, bf, is set. 3. an ack pulse is generated. 4. mssp interrupt flag bit, sspif of the pir1 reg- ister, is set (interrupt is generated, if enabled) on the falling edge of the ninth scl pulse. in 10-bit address mode, two address bytes need to be received by the slave. the five most significant bits (msbs) of the first address byte specify if this is a 10-bit address. bit r/w of the sspstat register must specify a write so the slave device will receive the second address byte. for a 10-bit address, the first byte would equal ? 11110 a9 a8 0 ?, where ? a9 ? and ? a8 ? are the two msbs of the address. the sequence of events for 10-bit address is as follows, with steps 7 through 9 for the slave-transmitter: 1. receive first (high) byte of address (bits sspif, bf and ua of the sspstat register are set). 2. read the sspbuf register (clears bit bf) and clear flag bit, sspif. 3. update the sspadd register with second (low) byte of address (clears bit ua and releases the scl line). 4. receive second (low) byte of address (bits sspif, bf and ua are set). if the address matches then the scl is held until the next step. otherwise the scl line is not held. 5. read the sspbuf register (clears bit bf) and clear flag bit, sspif. 6. update the sspadd register with the first (high) byte of address. (this will clear bit ua and release a held scl line.) 7. receive repeated start condition. 8. receive first (high) byte of address with r/w bit set (bits sspif, bf, r/w are set). 9. read the sspbuf register (clears bit bf) and clear flag bit, sspif. 10. load sspbuf with byte the slave is to transmit, sets the bf bit. 11. set the ckp bit to release scl. note: to ensure proper operation of the module, pull-up resistors must be provided exter- nally to the scl and sda pins.
? 2010 microchip technology inc. preliminary ds41350e-page 153 pic18f/lf1xk50 15.3.3.2 reception when the r/w bit of the address byte is clear and an address match occurs, the r/w bit of the sspstat register is cleared. the received address is loaded into the sspbuf register and the sda line is held low (ack ). when the address byte overflow condition exists, then the no acknowledge (ack ) pulse is given. an overflow condition is defined as either bit bf bit of the sspstat register is set, or bit sspov bit of the sspcon1 register is set. an mssp interrupt is generated for each data transfer byte. flag bit, sspif of the pir1 register, must be cleared by software. when the sen bit of the sspcon2 register is set, scl will be held low (clock stretch) following each data transfer. the clock must be released by setting the ckp bit of the sspcon1 register. see section 15.3.4 ?clock stretching? for more detail. 15.3.3.3 transmission when the r/w bit of the incoming address byte is set and an address match occurs, the r/w bit of the sspstat register is set. the received address is loaded into the sspbuf register. the ack pulse will be sent on the ninth bit and pin sck/scl is held low regardless of sen (see section 15.3.4 ?clock stretching? for more detail). by stretching the clock, the master will be unable to assert another clock pulse until the slave is done preparing the transmit data. the transmit data must be loaded into the sspbuf register which also loads the sspsr register. then pin sck/scl should be released by setting the ckp bit of the sspcon1 register. the eight data bits are shifted out on the falling edge of the scl input. this ensures that the sda signal is valid during the scl high time ( figure 15-9 ). the ack pulse from the master-receiver is latched on the rising edge of the ninth scl input pulse. if the sda line is high (not ack ), then the data transfer is complete. in this case, when the ack is latched by the slave, the slave logic is reset (resets sspstat register) and the slave monitors for another occurrence of the start bit. if the sda line was low (ack ), the next transmit data must be loaded into the sspbuf register. again, pin sck/scl must be released by setting bit ckp. an mssp interrupt is generated for each data transfer byte. the sspif bit must be cleared by software and the sspstat register is used to determine the status of the byte. the sspif bit is set on the falling edge of the ninth clock pulse.
pic18f/lf1xk50 ds41350e-page 154 preliminary ? 2010 microchip technology inc. figure 15-8: i 2 c? slave mode timing with sen = 0 (reception, 7-bit address) sda scl sspif bf (sspstat<0>) sspov (sspcon1<6>) s 12345678912345678912345 789 p a7 a6 a5 a4 a3 a2 a1 d7 d6 d5 d4 d3 d2 d1 d0 d7 d6 d5 d4 d3 d1 d0 ack receiving data ack receiving data r/w = 0 ack receiving address cleared by software sspbuf is read bus master terminates transfer sspov is set because sspbuf is still full. ack is not sent. d2 6 (pir1<3>) ckp (ckp does not reset to ? 0 ? when sen = 0 )
? 2010 microchip technology inc. preliminary ds41350e-page 155 pic18f/lf1xk50 figure 15-9: i 2 c? slave mode timing (transmission, 7-bit address) sda scl sspif (pir1<3>) bf (sspstat<0>) a6 a5 a4 a3 a2 a1 d6 d5 d4 d3 d2 d1 d0 1 2 3 4 5 6 7 8 2 3 4 5 6 7 8 9 sspbuf is written by software cleared by software from sspif isr data in sampled s ack transmitting data r/w = 0 ack receiving address a7 d7 9 1 d6 d5 d4 d3 d2 d1 d0 2 3 4 5 6 7 8 9 sspbuf is written by software cleared by software from sspif isr transmitting data d7 1 ckp p ack ckp is set by software ckp is set by software scl held low while cpu responds to sspif sspbuf is read by software bus master terminates software
pic18f/lf1xk50 ds41350e-page 156 preliminary ? 2010 microchip technology inc. figure 15-10: i 2 c? slave mode timing with sen = 0 (reception, 10-bit address) sda scl sspif bf (sspstat<0>) s 123456789 123456789 12345 789 p 11110 a9 a8 a7 a6 a5 a4 a3 a2 a1 a0 d7 d6 d5 d4 d3 d1 d0 receive data byte ack r/w = 0 ack receive first byte of address cleared by software d2 6 (pir1<3>) cleared by software receive second byte of address cleared by hardware when sspadd is updated with low byte of address ua (sspstat<1>) clock is held low until update of sspadd has taken place ua is set indicating that the sspadd needs to be updated ua is set indicating that sspadd needs to be updated cleared by hardware when sspadd is updated with high byte of address sspbuf is written with contents of sspsr dummy read of sspbuf to clear bf flag ack ckp 12345 789 d7 d6 d5 d4 d3 d1 d0 receive data byte bus master terminates transfer d2 6 ack cleared by software cleared by software sspov (sspcon1<6>) sspov is set because sspbuf is still full. ack is not sent. (ckp does not reset to ? 0 ? when sen = 0 ) clock is held low until update of sspadd has taken place
? 2010 microchip technology inc. preliminary ds41350e-page 157 pic18f/lf1xk50 figure 15-11: i 2 c? slave mode timing (tra nsmission, 10-bit address) sda scl sspif bf s 1 234 56 7 89 1 2345 67 89 1 2345 7 89 p 11110 a9 a8 a7 a6 a5 a4 a3 a2 a1 a0 11110 a8 r/w =1 ack ack r/w = 0 ack receive first byte of address cleared in software bus master sends stop condition a9 6 receive second byte of address cleared by hardware when sspadd is updated with low byte of address. ua clock is held low until update of sspadd has taken place ua is set indicating that the sspadd needs to be updated ua is set indicating that sspadd needs to be updated cleared by hardware when sspadd is updated with high byte of address. sspbuf is written with contents of sspsr dummy read of sspbuf to clear bf flag receive first byte of address 12345 789 d7 d6 d5 d4 d3 d1 ack d2 6 transmitting data byte d0 dummy read of sspbuf to clear bf flag sr cleared in software write of sspbuf cleared in software completion of clears bf flag ckp ckp is set in software, initiates transmission ckp is automatically cleared in hardware holding scl low clock is held low until update of sspadd has taken place data transmission clock is held low until ckp is set to ?1? bus master sends restarts condition dummy read of sspbuf to clear bf flag
pic18f/lf1xk50 ds41350e-page 158 preliminary ? 2010 microchip technology inc. 15.3.3.4 ssp mask register an ssp mask (sspmsk) register is available in i 2 c slave mode as a mask for the value held in the sspsr register during an address comparison operation. a zero (? 0 ?) bit in the sspmsk register has the effect of making the corresponding bit in the sspsr register a ?don?t care?. this register is reset to all ? 1 ?s upon any reset condition and, therefore, has no effect on standard ssp operation until written with a mask value. this register must be initiated prior to setting sspm<3:0> bits to select the i 2 c slave mode (7-bit or 10-bit address). the ssp mask register is active during: ? 7-bit address mode: address compare of a<7:1>. ? 10-bit address mode: address compare of a<7:0> only. the ssp mask has no effect during the reception of the first (high) byte of the address. register 15-6: sspmsk: ssp mask register r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 msk7 msk6 msk5 msk4 msk3 msk2 msk1 msk0 (1) bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-1 msk<7:1>: mask bits 1 = the received address bit n is compared to sspadd to detect i 2 c address match 0 = the received address bit n is not used to detect i 2 c address match bit 0 msk<0>: mask bit for i 2 c slave mode, 10-bit address (1) i 2 c slave mode, 10-bit address (sspm<3:0> = 0111 ): 1 = the received address bit 0 is compared to sspadd<0> to detect i 2 c address match 0 = the received address bit 0 is not used to detect i 2 c address match note 1: the msk0 bit is used only in 10-bit slave mode. in all other modes, this bit has no effect.
? 2010 microchip technology inc. preliminary ds41350e-page 159 pic18f/lf1xk50 register 15-7: sspadd: mssp address and baud rate register (i 2 c mode) r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 add7 add6 add5 add4 add3 add2 add1 add0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown master mode : bit 7-0 add<7:0>: baud rate clock divider bits scl pin clock period = ((add<7:0> + 1) *4)/f osc 10-bit slave mode ? m ost significant address byte : bit 7-3 not used: unused for most significant address byte. bit state of this register is a ?don?t care.? bit pat- tern sent by master is fixed by i 2 c specification and must be equal to ? 11110 ?. however, those bits are compared by hardware and are not affected by the value in this register. bit 2-1 add<9:8>: two most significant bits of 10-bit address bit 0 not used: unused in this mode. bit state is a ?don?t care.? 10-bit slave mode ? l east significant address byte : bit 7-0 add<7:0>: eight least significant bits of 10-bit address 7-bit slave mode : bit 7-1 add<6:0>: 7-bit address bit 0 not used: unused in this mode. bit state is a ?don?t care.?
pic18f/lf1xk50 ds41350e-page 160 preliminary ? 2010 microchip technology inc. 15.3.4 clock stretching both 7-bit and 10-bit slave modes implement automatic clock stretching during a transmit sequence. the sen bit of the sspcon2 register allows clock stretching to be enabled during receives. setting sen will cause the scl pin to be held low at the end of each data receive sequence. 15.3.4.1 clock stretching for 7-bit slave receive mode (sen = 1 ) in 7-bit slave receive mode, on the falling edge of the ninth clock at the end of the ack sequence if the bf bit is set, the ckp bit of the sspcon1 register is automatically cleared, forcing the scl output to be held low. the ckp being cleared to ? 0 ? will assert the scl line low. the ckp bit must be set in the user?s isr before reception is allowed to continue. by holding the scl line low, the user has time to service the isr and read the contents of the sspbuf before the master device can initiate another data transfer sequence. this will prevent buffer overruns from occurring (see figure 15-13 ). 15.3.4.2 clock stretching for 10-bit slave receive mode (sen = 1 ) in 10-bit slave receive mode during the address sequence, clock stretching automatically takes place but ckp is not cleared. during this time, if the ua bit is set after the ninth clock, clock stretching is initiated. the ua bit is set after receiving the upper byte of the 10-bit address and following the receive of the second byte of the 10-bit address with the r/w bit cleared to ? 0 ?. the release of the clock line occurs upon updating sspadd. clock stretching will occur on each data receive sequence as described in 7-bit mode. 15.3.4.3 clock stretching for 7-bit slave transmit mode 7-bit slave transmit mode implements clock stretching by clearing the ckp bit after the falling edge of the ninth clock. this occurs regardless of the state of the sen bit. the user?s isr must set the ckp bit before transmis- sion is allowed to continue. by holding the scl line low, the user has time to service the isr and load the contents of the sspbuf before the master device can initiate another data transfer sequence (see figure 15-9 ). 15.3.4.4 clock stretching for 10-bit slave transmit mode in 10-bit slave transmit mode, clock stretching is con- trolled during the first two address sequences by the state of the ua bit, just as it is in 10-bit slave receive mode. the first two addresses are followed by a third address sequence which contains the high-order bits of the 10-bit address and the r/w bit set to ? 1 ?. after the third address sequence is performed, the ua bit is not set, the module is now configured in transmit mode and clock stretching is automatic with the hard- ware clearing ckp, as in 7-bit slave transmit mode (see figure 15-11 ). note 1: if the user reads the contents of the sspbuf before the falling edge of the ninth clock, thus clearing the bf bit, the ckp bit will not be cleared and clock stretching will not occur. 2: the ckp bit can be set by software regardless of the state of the bf bit. the user should be careful to clear the bf bit in the isr before the next receive sequence in order to prevent an overflow condition. note 1: if the user loads the contents of sspbuf, setting the bf bit before the falling edge of the ninth clock, the ckp bit will not be cleared and clock stretching will not occur. 2: the ckp bit can be set by software regardless of the state of the bf bit.
? 2010 microchip technology inc. preliminary ds41350e-page 161 pic18f/lf1xk50 15.3.4.5 clock synchronization and the ckp bit when the ckp bit is cleared, the scl output is forced to ? 0 ?. however, clearing the ckp bit will not assert the scl output low until the scl output is already sam- pled low. therefore, the ckp bit will not assert the scl line until an external i 2 c master device has already asserted the scl line. the scl output will remain low until the ckp bit is set and all other devices on the i 2 c bus have deasserted scl. this ensures that a write to the ckp bit will not violate the minimum high time requirement for scl (see figure 15-12 ). figure 15-12: clock synchronization timing sda scl dx ? 1 dx wr q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 sspcon1 ckp master device deasserts clock master device asserts clock
pic18f/lf1xk50 ds41350e-page 162 preliminary ? 2010 microchip technology inc. figure 15-13: i 2 c? slave mode timing with sen = 1 (reception, 7-bit address) sda scl sspif bf (sspstat<0>) sspov (sspcon1<6>) s 1 234 56 7 8 9 1 2345 67 89 1 23 45 7 89 p a7 a6 a5 a4 a3 a2 a1 d7 d6 d5 d4 d3 d2 d1 d0 d7 d6 d5 d4 d3 d1 d0 ack receiving data ack receiving data r/w = 0 ack receiving address cleared by software sspbuf is read bus master terminates transfer sspov is set because sspbuf is still full. ack is not sent. d2 6 (pir1<3>) ckp ckp written to ? 1 ? in if bf is cleared prior to the falling edge of the 9th clock, ckp will not be reset to ? 0 ? and no clock stretching will occur software clock is held low until ckp is set to ? 1 ? clock is not held low because buffer full bit is clear prior to falling edge of 9th clock clock is not held low because ack = 1 bf is set after falling edge of the 9th clock, ckp is reset to ? 0 ? and clock stretching occurs
? 2010 microchip technology inc. preliminary ds41350e-page 163 pic18f/lf1xk50 figure 15-14: i 2 c? slave mode timing with sen = 1 (reception, 10-bit address) sda scl sspif bf (sspstat<0>) s 123456789 123456789 12345 789 p 11110 a9 a8 a7 a6 a5 a4 a3 a2 a1 a0 d7 d6 d5 d4 d3 d1 d0 receive data byte ack r/w = 0 ack receive first byte of address cleared by software d2 6 (pir1<3>) cleared by software receive second byte of address cleared by hardware when sspadd is updated with low byte of address after falling edge ua (sspstat<1>) clock is held low until update of sspadd has taken place ua is set indicating that the sspadd needs to be updated ua is set indicating that sspadd needs to be updated cleared by hardware when sspadd is updated with high byte of address after falling edge sspbuf is written with contents of sspsr dummy read of sspbuf to clear bf flag ack ckp 12345 789 d7 d6 d5 d4 d3 d1 d0 receive data byte bus master terminates transfer d2 6 ack cleared by software cleared by software sspov (sspcon1<6>) ckp written to ? 1 ? note: an update of the sspadd register before the falling edge of the ninth clock will have no effect on ua and ua will remain set. note: an update of the sspadd register before the falling edge of the ninth clock will have no effect on ua and ua will remain set. by software clock is held low until update of sspadd has taken place of ninth clock of ninth clock sspov is set because sspbuf is still full. ack is not sent. dummy read of sspbuf to clear bf flag clock is held low until ckp is set to ? 1 ? clock is not held low because ack = 1
pic18f/lf1xk50 ds41350e-page 164 preliminary ? 2010 microchip technology inc. 15.3.5 general call address support the addressing procedure for the i 2 c bus is such that the first byte after the start condition usually determines which device will be the slave addressed by the master. the exception is the general call address which can address all devices. when this address is used, all devices should, in theory, respond with an acknowledge. the general call address is one of eight addresses reserved for specific purposes by the i 2 c protocol. it consists of all ? 0 ?s with r/w = 0 . the general call address is recognized when the gcen bit of the sspcon2 is set. following a start bit detect, 8 bits are shifted into the sspsr and the address is compared against the sspadd. it is also compared to the general call address and fixed in hardware. if the general call address matches, the sspsr is transferred to the sspbuf, the bf flag bit is set (eighth bit) and on the falling edge of the ninth bit (ack bit), the sspif interrupt flag bit is set. when the interrupt is serviced, the source for the interrupt can be checked by reading the contents of the sspbuf. the value can be used to determine if the address was device specific or a general call address. in 10-bit mode, the sspadd is required to be updated for the second half of the address to match and the ua bit of the sspstat register is set. if the general call address is sampled when the gcen bit is set, while the slave is configured in 10-bit address mode, then the second half of the address is not necessary, the ua bit will not be set and the slave will begin receiving data after the acknowledge ( figure 15-15 ). figure 15-15: slave mode general call address sequence (7 or 10-bit address mode) sda scl s sspif bf (sspstat<0>) sspov (sspcon1<6>) cleared by software sspbuf is read r/w = 0 ack general call address address is compared to general call address gcen (sspcon2<7>) receiving data ack 123456789123456789 d7 d6 d5 d4 d3 d2 d1 d0 after ack , set interrupt ? 0 ? ? 1 ?
? 2010 microchip technology inc. preliminary ds41350e-page 165 pic18f/lf1xk50 15.3.6 master mode master mode is enabled by setting and clearing the appropriate sspm bits in sspcon1 and by setting the sspen bit. in master mode, the scl and sda lines are manipulated by the mssp hardware. master mode of operation is supported by interrupt generation on the detection of the start and stop con- ditions. the stop (p) and start (s) bits are cleared from a reset or when the mssp module is disabled. control of the i 2 c bus may be taken when the p bit is set, or the bus is idle, with both the s and p bits clear. in firmware controlled master mode, user code conducts all i 2 c bus operations based on start and stop bit conditions. once master mode is enabled, the user has six options. 1. assert a start condition on sda and scl. 2. assert a repeated start condition on sda and scl. 3. write to the sspbuf register initiating transmission of data/address. 4. configure the i 2 c port to receive data. 5. generate an acknowledge condition at the end of a received byte of data. 6. generate a stop condition on sda and scl. the following events will cause the ssp interrupt flag bit, sspif, to be set (ssp interrupt, if enabled): ? start condition ? stop condition ? data transfer byte transmitted/received ? acknowledge transmit ? repeated start figure 15-16: mssp block diagram (i 2 c? master mode) note: the mssp module, when configured in i 2 c master mode, does not allow queueing of events. for instance, the user is not allowed to initiate a start condition and immediately write the sspbuf register to initiate transmission before the start condition is complete. in this case, the sspbuf will not be written to and the wcol bit will be set, indicating that a write to the sspbuf did not occur. read write sspsr start bit, stop bit, sspbuf internal data bus set/reset, s, p, wcol shift clock msb lsb sda acknowledge generate stop bit detect write collision detect clock arbitration state counter for end of xmit/rcv scl scl in bus collision sda in receive enable clock cntl clock arbitrate/wcol detect (hold off clock source) sspadd<6:0> baud set sspif, bclif reset ackstat, pen rate generator sspm<3:0> start bit detect
pic18f/lf1xk50 ds41350e-page 166 preliminary ? 2010 microchip technology inc. 15.3.6.1 i 2 c master mode operation the master device generates all of the serial clock pulses and the start and stop conditions. a transfer is ended with a stop condition or with a repeated start condition. since the repeated start condition is also the beginning of the next serial transfer, the i 2 c bus will not be released. in master transmitter mode, serial data is output through sda, while scl outputs the serial clock. the first byte transmitted contains the slave address of the receiving device (7 bits) and the read/write (r/w ) bit. in this case, the r/w bit will be logic ? 0 ?. serial data is transmitted 8 bits at a time. after each byte is transmit- ted, an acknowledge bit is received. start and stop conditions are output to indicate the beginning and the end of a serial transfer. in master receive mode, the first byte transmitted con- tains the slave address of the transmitting device (7 bits) and the r/w bit. in this case, the r/w bit will be logic ? 1 ?. thus, the first byte transmitted is a 7-bit slave address followed by a ? 1 ? to indicate the receive bit. serial data is received via sda, while scl outputs the serial clock. serial data is received 8 bits at a time. after each byte is received, an acknowledge bit is transmit- ted. start and stop conditions indicate the beginning and end of transmission. a baud rate generator is used to set the clock frequency output on scl. see section 15.3.7 ?baud rate? for more detail. a typical transmit sequence would go as follows: 1. the user generates a start condition by setting the sen bit of the sspcon2 register. 2. sspif is set. the mssp module will wait the required start time before any other operation takes place. 3. the user loads the sspbuf with the slave address to transmit. 4. address is shifted out the sda pin until all 8 bits are transmitted. 5. the mssp module shifts in the ack bit from the slave device and writes its value into the ackstat bit of the sspcon2 register. 6. the mssp module generates an interrupt at the end of the ninth clock cycle by setting the sspif bit. 7. the user loads the sspbuf with eight bits of data. 8. data is shifted out the sda pin until all 8 bits are transmitted. 9. the mssp module shifts in the ack bit from the slave device and writes its value into the ackstat bit of the sspcon2 register. 10. the mssp module generates an interrupt at the end of the ninth clock cycle by setting the sspif bit. 11. the user generates a stop condition by setting the pen bit of the sspcon2 register. 12. interrupt is generated once the stop condition is complete.
? 2010 microchip technology inc. preliminary ds41350e-page 167 pic18f/lf1xk50 15.3.7 baud rate in i 2 c master mode, the baud rate generator (brg) reload value is placed in the sspadd register ( figure 15-17 ). when a write occurs to sspbuf, the baud rate generator will automatically begin counting. once the given operation is complete (i.e., transmission of the last data bit is followed by ack ), the internal clock will automatically stop counting and the scl pin will remain in its last state. table 15-3 demonstrates clock rates based on instruction cycles and the brg value loaded into sspadd. equation 15-1: figure 15-17: baud rate generato r block diagram table 15-3: i 2 c? clock rate w/brg f scl f osc sspadd 1 + ?? 4 ?? ---------------------------------------------- = sspm<3:0> brg down counter clkout f osc /2 sspadd<7:0> sspm<3:0> scl reload control reload f osc f cy brg value f scl (2 rollovers of brg) 48 mhz 12 mhz 0bh 1 mhz (1) 48 mhz 12 mhz 1dh 400 khz 48 mhz 12 mhz 77h 100 khz 40 mhz 10 mhz 18h 400 khz (1) 40 mhz 10 mhz 1fh 312.5 khz 40 mhz 10 mhz 63h 100 khz 16 mhz 4 mhz 09h 400 khz (1) 16 mhz 4 mhz 0ch 308 khz 16 mhz 4 mhz 27h 100 khz 4 mhz 1 mhz 02h 333 khz (1) 4 mhz 1 mhz 09h 100 khz 4 mhz 1 mhz 00h 1 mhz (1) note 1: the i 2 c interface does not conform to the 400 khz i 2 c specification (which applies to rates greater than 100 khz) in all details, but may be used with care where higher rates are required by the application.
pic18f/lf1xk50 ds41350e-page 168 preliminary ? 2010 microchip technology inc. 15.3.7.1 clock arbitration clock arbitration occurs when the master, during any receive, transmit or repeated start/stop condition, deasserts the scl pin (scl allowed to float high). when the scl pin is allowed to float high, the baud rate generator (brg) is suspended from counting until the scl pin is actually sampled high. when the scl pin is sampled high, the baud rate generator is reloaded with the contents of sspadd<6:0> and begins counting. this ensures that the scl high time will always be at least one brg rollover count in the event that the clock is held low by an external device ( figure 15-18 ). figure 15-18: baud rate generator timing with clock arbitration sda scl scl deasserted but slave holds dx ? 1 dx brg scl is sampled high, reload takes place and brg starts its count 03h 02h 01h 00h (hold off) 03h 02h reload brg value scl low (clock arbitration) scl allowed to transition high brg decrements on q2 and q4 cycles
? 2010 microchip technology inc. preliminary ds41350e-page 169 pic18f/lf1xk50 15.3.8 i 2 c master mode start condition timing to initiate a start condition, the user sets the start enable bit, sen bit of the sspcon2 register. if the sda and scl pins are sampled high, the baud rate generator is reloaded with the contents of sspadd<6:0> and starts its count. if scl and sda are both sampled high when the baud rate generator times out (t brg ), the sda pin is driven low. the action of the sda being driven low while scl is high is the start condition and causes the s bit of the sspstat1 register to be set. following this, the baud rate gener- ator is reloaded with the contents of sspadd<7:0> and resumes its count. when the baud rate generator times out (t brg ), the sen bit of the sspcon2 register will be automatically cleared by hardware; the baud rate generator is suspended, leaving the sda line held low and the start condition is complete. 15.3.8.1 wcol status flag if the user writes the sspbuf when a start sequence is in progress, the wcol is set and the contents of the buffer are unchanged (the write doesn?t occur). figure 15-19: first start bit timing note: if at the beginning of the start condition, the sda and scl pins are already sam- pled low, or if during the start condition, the scl line is sampled low before the sda line is driven low, a bus collision occurs, the bus collision interrupt flag, bclif, is set, the start condition is aborted and the i 2 c module is reset into its idle state. note: because queueing of events is not allowed, writing to the lower 5 bits of sspcon2 is disabled until the start condition is complete. sda scl s t brg 1st bit 2nd bit t brg sda = 1 , at completion of start bit, scl = 1 write to sspbuf occurs here t brg hardware clears sen bit t brg write to sen bit occurs here set s bit (sspstat<3>) and sets sspif bit
pic18f/lf1xk50 ds41350e-page 170 preliminary ? 2010 microchip technology inc. 15.3.9 i 2 c master mode repeated start condition timing a repeated start condition occurs when the rsen bit of the sspcon2 register is programmed high and the i 2 c logic module is in the idle state. when the rsen bit is set, the scl pin is asserted low. when the scl pin is sampled low, the baud rate generator is loaded and begins counting. the sda pin is released (brought high) for one baud rate generator count (t brg ). when the baud rate generator times out, if sda is sampled high, the scl pin will be deasserted (brought high). when scl is sampled high, the baud rate generator is reloaded and begins counting. sda and scl must be sampled high for one t brg . this action is then fol- lowed by assertion of the sda pin (sda = 0 ) for one t brg while scl is high. following this, the rsen bit of the sspcon2 register will be automatically cleared and the baud rate generator will not be reloaded, leaving the sda pin held low. as soon as a start condi- tion is detected on the sda and scl pins, the s bit of the sspstat register will be set. the sspif bit will not be set until the baud rate generator has timed out. immediately following the sspif bit getting set, the user may write the sspbuf with the 7-bit address in 7-bit mode or the default first address in 10-bit mode. after the first eight bits are transmitted and an ack is received, the user may then transmit an additional eight bits of address (10-bit mode) or eight bits of data (7-bit mode). 15.3.9.1 wcol status flag if the user writes the sspbuf when a repeated start sequence is in progress, the wcol is set and the contents of the buffer are unchanged (the write doesn?t occur). figure 15-20: repeat start condition waveform note 1: if rsen is programmed while any other event is in progress, it will not take effect. 2: a bus collision during the repeated start condition occurs if: ? sda is sampled low when scl goes from low-to-high. ? scl goes low before sda is asserted low. this may indicate that another master is attempting to transmit a data ? 1 ?. note: because queueing of events is not allowed, writing of the lower 5 bits of sspcon2 is disabled until the repeated start condition is complete. sda scl sr = repeated start write to sspcon2 write to sspbuf occurs here on falling edge of ninth clock, end of xmit at completion of start bit, hardware clears rsen bit 1st bit s bit set by hardware t brg t brg sda = 1 , sda = 1 , scl (no change). scl = 1 occurs here. t brg t brg t brg and sets sspif rsen bit set by hardware
? 2010 microchip technology inc. preliminary ds41350e-page 171 pic18f/lf1xk50 15.3.10 i 2 c master mode transmission transmission of a data byte, a 7-bit address or the other half of a 10-bit address is accomplished by simply writing a value to the sspbuf register. this action will set the buffer full flag bit, bf and allow the baud rate generator to begin counting and start the next trans- mission. each bit of address/data will be shifted out onto the sda pin after the falling edge of scl is asserted (see data hold time specification parameter sp106). scl is held low for one baud rate generator rollover count (t brg ). data should be valid before scl is released high (see data setup time spec- ification parameter sp107). when the scl pin is released high, it is held that way for t brg . the data on the sda pin must remain stable for that duration and some hold time after the next falling edge of scl. after the eighth bit is shifted out (the falling edge of the eighth clock), the bf flag is cleared and the master releases sda. this allows the slave device being addressed to respond with an ack bit during the ninth bit time if an address match occurred, or if data was received prop- erly. the status of ack is written into the ackdt bit on the falling edge of the ninth clock. if the master receives an acknowledge, the acknowledge status bit, ackstat, is cleared. if not, the bit is set. after the ninth clock, the sspif bit is set and the master clock (baud rate generator) is suspended until the next data byte is loaded into the sspbuf, leaving scl low and sda unchanged ( figure 15-21 ). after the write to the sspbuf, each bit of the address will be shifted out on the falling edge of scl until all seven address bits and the r/w bit are completed. on the falling edge of the eighth clock, the master will deassert the sda pin, allowing the slave to respond with an acknowledge. on the falling edge of the ninth clock, the master will sample the sda pin to see if the address was recognized by a slave. the status of the ack bit is loaded into the ackstat status bit of the sspcon2 register. following the falling edge of the ninth clock transmission of the address, the sspif is set, the bf flag is cleared and the baud rate generator is turned off until another write to the sspbuf takes place, holding scl low and allowing sda to float. 15.3.10.1 bf status flag in transmit mode, the bf bit of the sspstat register is set when the cpu writes to sspbuf and is cleared when all 8 bits are shifted out. 15.3.10.2 wcol status flag if the user writes the sspbuf when a transmit is already in progress (i.e., sspsr is still shifting out a data byte), the wcol is set and the contents of the buf- fer are unchanged (the write doesn?t occur). wcol must be cleared by software before the next transmission. 15.3.10.3 ackstat status flag in transmit mode, the ackstat bit of the sspcon2 register is cleared when the slave has sent an acknowl- edge (ack = 0 ) and is set when the slave does not acknowledge (ack = 1 ). a slave sends an acknowl- edge when it has recognized its address (including a general call), or when the slave has properly received its data. 15.3.11 i 2 c master mode reception master mode reception is enabled by programming the receive enable bit, rcen bit of the sspcon2 register. the baud rate generator begins counting and on each rollover, the state of the scl pin changes (high-to-low/low-to-high) and data is shifted into the sspsr. after the falling edge of the eighth clock, the receive enable flag is automatically cleared, the con- tents of the sspsr are loaded into the sspbuf, the bf flag bit is set, the sspif flag bit is set and the baud rate generator is suspended from counting, holding scl low. the mssp is now in idle state awaiting the next command. when the buffer is read by the cpu, the bf flag bit is automatically cleared. the user can then send an acknowledge bit at the end of reception by setting the acknowledge sequence enable, acken bit of the sspcon2 register. 15.3.11.1 bf status flag in receive operation, the bf bit is set when an address or data byte is loaded into sspbuf from sspsr. it is cleared when the sspbuf register is read. 15.3.11.2 sspov status flag in receive operation, the sspov bit is set when 8 bits are received into the sspsr and the bf flag bit is already set from a previous reception. 15.3.11.3 wcol status flag if the user writes the sspbuf when a receive is already in progress (i.e., sspsr is still shifting in a data byte), the wcol bit is set and the contents of the buffer are unchanged (the write doesn?t occur). note: the mssp module must be in an idle state before the rcen bit is set or the rcen bit will be disregarded.
pic18f/lf1xk50 ds41350e-page 172 preliminary ? 2010 microchip technology inc. figure 15-21: i 2 c? master mode waveform (transmission, 7 or 10-bit address) sda scl sspif bf (sspstat<0>) sen a7 a6 a5 a4 a3 a2 a1 ack = 0 d7 d6 d5 d4 d3 d2 d1 d0 ack transmitting data or second half r/w = 0 transmit address to slave 123456789 123456789 p cleared by software service routine sspbuf is written by software from ssp interrupt after start condition, sen cleared by hardware s sspbuf written with 7-bit address and r/w start transmit scl held low while cpu responds to sspif sen = 0 of 10-bit address write sspcon2<0> sen = 1 start condition begins from slave, clear ackstat bit sspcon2<6> ackstat in sspcon2 = 1 cleared by software sspbuf written pen r/w cleared by software
? 2010 microchip technology inc. preliminary ds41350e-page 173 pic18f/lf1xk50 figure 15-22: i 2 c? master mode waveform (reception, 7-bit address) p 9 8 7 6 5 d0 d1 d2 d3 d4 d5 d6 d7 s a7 a6 a5 a4 a3 a2 a1 sda scl 12 3 4 5 6 7 8 9 12 3 4 5 678 9 1234 bus master terminates transfer ack receiving data from slave receiving data from slave d0 d1 d2 d3 d4 d5 d6 d7 ack r/w = 0 transmit address to slave sspif bf ack is not sent write to sspcon2<0> (sen = 1 ), write to sspbuf occurs here, ack from slave master configured as a receiver by programming sspcon2<3> (rcen = 1 ) pen bit = 1 written here data shifted in on falling edge of clk cleared by software start xmit sen = 0 sspov sda = 0 , scl = 1 while cpu (sspstat<0>) ack cleared by software cleared by software set sspif interrupt at end of receive set p bit (sspstat<4>) and sspif cleared in software ack from master set sspif at end set sspif interrupt at end of acknowledge sequence set sspif interrupt at end of acknow- ledge sequence of receive set acken, start acknowledge sequence sspov is set because sspbuf is still full sda = ackdt = 1 rcen cleared automatically rcen = 1 , start next receive write to sspcon2<4> to start acknowledge sequence sda = ackdt (sspcon2<5>) = 0 rcen cleared automatically responds to sspif acken begin start condition cleared by software sda = ackdt = 0 last bit is shifted into sspsr and contents are unloaded into sspbuf rcen master configured as a receiver by programming sspcon2<3> (rcen = 1 ) rcen cleared automatically ack from master sda = ackdt = 0 rcen cleared automatically
pic18f/lf1xk50 ds41350e-page 174 preliminary ? 2010 microchip technology inc. 15.3.12 acknowledge sequence timing an acknowledge sequence is enabled by setting the acknowledge sequence enable bit, acken bit of the sspcon2 register. when this bit is set, the scl pin is pulled low and the contents of the acknowledge data bit are presented on the sda pin. if the user wishes to gen- erate an acknowledge, then the ackdt bit should be cleared. if not, the user should set the ackdt bit before starting an acknowledge sequence. the baud rate generator then counts for one rollover period (t brg ) and the scl pin is deasserted (pulled high). when the scl pin is sampled high (clock arbitration), the baud rate generator counts for t brg . the scl pin is then pulled low. following this, the acken bit is automatically cleared, the baud rate generator is turned off and the mssp module then goes into idle mode ( figure 15-23 ). 15.3.12.1 wcol status flag if the user writes the sspbuf when an acknowledge sequence is in progress, then wcol is set and the contents of the buffer are unchanged (the write doesn?t occur). 15.3.13 stop condition timing a stop bit is asserted on the sda pin at the end of a receive/transmit by setting the stop sequence enable bit, pen bit of the sspcon2 register. at the end of a receive/transmit, the scl line is held low after the falling edge of the ninth clock. when the pen bit is set, the master will assert the sda line low. when the sda line is sampled low, the baud rate generator is reloaded and counts down to ? 0 ?. when the baud rate generator times out, the scl pin will be brought high and one t brg (baud rate generator rollover count) later, the sda pin will be deasserted. when the sda pin is sampled high while scl is high, the p bit of the sspstat register is set. a t brg later, the pen bit is cleared and the sspif bit is set ( figure 15-24 ). 15.3.13.1 wcol status flag if the user writes the sspbuf when a stop sequence is in progress, then the wcol bit is set and the contents of the buffer are unchanged (the write doesn?t occur). figure 15-23: acknowledge sequen ce waveform figure 15-24: stop cond ition receive or transmit mode note: t brg = one baud rate generator period. sda scl sspif set at acknowledge sequence starts here, write to sspcon2 acken automatically cleared cleared in t brg t brg the end of receive 8 acken = 1 , ackdt = 0 d0 9 sspif software sspif set at the end of acknowledge sequence cleared in software ack scl sda sda asserted low before rising edge of clock write to sspcon2, set pen falling edge of scl = 1 for t brg , followed by sda = 1 for t brg 9th clock scl brought high after t brg note: t brg = one baud rate generator period. t brg t brg after sda sampled high. p bit (sspstat<4>) is set. t brg to setup stop condition ack p t brg pen bit (sspcon2<2>) is cleared by hardware and the sspif bit is set
? 2010 microchip technology inc. preliminary ds41350e-page 175 pic18f/lf1xk50 15.3.14 sleep operation while in sleep mode, the i 2 c slave module can receive addresses or data and when an address match or complete byte transfer occurs, wake the processor from sleep (if the mssp interrupt is enabled). 15.3.15 effects of a reset a reset disables the mssp module and terminates the current transfer. 15.3.16 multi-master mode in multi-master mode, the interrupt generation on the detection of the start and stop conditions allows the determination of when the bus is free. the stop (p) and start (s) bits are cleared from a reset or when the mssp module is disabled. control of the i 2 c bus may be taken when the p bit of the sspstat register is set, or the bus is idle, with both the s and p bits clear. when the bus is busy, enabling the ssp interrupt will gener- ate the interrupt when the stop condition occurs. in multi-master operation, the sda line must be monitored for arbitration to see if the signal level is the expected output level. this check is performed by hardware with the result placed in the bclif bit. the states where arbitration can be lost are: ? address transfer ? data transfer ? a start condition ? a repeated start condition ? an acknowledge condition 15.3.17 multi -master communication, bus collision and bus arbitration multi-master mode support is achieved by bus arbitra- tion. when the master outputs address/data bits onto the sda pin, arbitration takes place when the master outputs a ? 1 ? on sda, by letting sda float high and another master asserts a ? 0 ?. when the scl pin floats high, data should be stable. if the expected data on sda is a ? 1 ? and the data sampled on the sda pin = 0 , then a bus collision has taken place. the master will set the bus collision interrupt flag, bclif and reset the i 2 c port to its idle state ( figure 15-25 ). if a transmit was in progress when the bus collision occurred, the transmission is halted, the bf flag is cleared, the sda and scl lines are deasserted and the sspbuf can be written to. when the user services the bus collision interrupt service routine and if the i 2 c bus is free, the user can resume communication by asserting a start condition. if a start, repeated start, stop or acknowledge condi- tion was in progress when the bus collision occurred, the condition is aborted, the sda and scl lines are deas- serted and the respective control bits in the sspcon2 register are cleared. when the user services the bus col- lision interrupt service routine and if the i 2 c bus is free, the user can resume communication by asserting a start condition. the master will continue to monitor the sda and scl pins. if a stop condition occurs, the sspif bit will be set. a write to the sspbuf will start the transmission of data at the first data bit, regardless of where the transmitter left off when the bus collision occurred. in multi-master mode, the interrupt generation on the detection of start and stop conditions allows the deter- mination of when the bus is free. control of the i 2 c bus can be taken when the p bit is set in the sspstat register, or the bus is idle and the s and p bits are cleared. figure 15-25: bus collision timing for transmit and acknowledge sda scl bclif sda released sda line pulled low by another source sample sda. while scl is high, data doesn?t match what is driven bus collision has occurred. set bus collision interrupt (bclif) by the master. by master data changes while scl = 0
pic18f/lf1xk50 ds41350e-page 176 preliminary ? 2010 microchip technology inc. 15.3.17.1 bus collision during a start condition during a start condition, a bus collision occurs if: a) sda or scl are sampled low at the beginning of the start condition ( figure 15-26 ). b) scl is sampled low before sda is asserted low ( figure 15-27 ). during a start condition, both the sda and the scl pins are monitored. if the sda pin is already low, or the scl pin is already low, then all of the following occur: ? the start condition is aborted, ? the bclif flag is set and ? the mssp module is reset to its idle state ( figure 15-26 ). the start condition begins with the sda and scl pins deasserted. when the sda pin is sampled high, the baud rate generator is loaded and counts down. if the scl pin is sampled low while sda is high, a bus collision occurs because it is assumed that another master is attempting to drive a data ? 1 ? during the start condition. if the sda pin is sampled low during this count, the brg is reset and the sda line is asserted early ( figure 15-28 ). if, however, a ? 1 ? is sampled on the sda pin, the sda pin is asserted low at the end of the brg count. the baud rate generator is then reloaded and counts down to 0; if the scl pin is sampled as ? 0 ? during this time, a bus collision does not occur. at the end of the brg count, the scl pin is asserted low. figure 15-26: bus collision during start condition (sda only) note: the reason that bus collision is not a fac- tor during a start condition is that no two bus masters can assert a start condition at the exact same time. therefore, one master will always assert sda before the other. this condition does not cause a bus collision because the two masters must be allowed to arbitrate the first address fol- lowing the start condition. if the address is the same, arbitration must be allowed to continue into the data portion, repeated start or stop conditions. sda scl sen sda sampled low before sda goes low before the sen bit is set. s bit and sspif set because ssp module reset into idle state. sen cleared automatically because of bus collision. s bit and sspif set because set sen, enable start condition if sda = 1 , scl = 1 sda = 0 , scl = 1 . bclif s sspif sda = 0 , scl = 1 . sspif and bclif are cleared by software sspif and bclif are cleared by software set bclif, start condition. set bclif.
? 2010 microchip technology inc. preliminary ds41350e-page 177 pic18f/lf1xk50 figure 15-27: bus collision d uring start condition (scl = 0 ) figure 15-28: brg reset due to sda arbitration during start condition sda scl sen bus collision occurs. set bclif. scl = 0 before sda = 0 , set sen, enable start sequence if sda = 1 , scl = 1 t brg t brg sda = 0 , scl = 1 bclif s sspif interrupt cleared by software bus collision occurs. set bclif. scl = 0 before brg time-out, ? 0 ?? 0 ? ? 0 ? ? 0 ? sda scl sen set s less than t brg t brg sda = 0 , scl = 1 bclif s sspif s interrupts cleared by software set sspif sda = 0 , scl = 1 , scl pulled low after brg time-out set sspif ? 0 ? sda pulled low by other master. reset brg and assert sda. set sen, enable start sequence if sda = 1 , scl = 1
pic18f/lf1xk50 ds41350e-page 178 preliminary ? 2010 microchip technology inc. 15.3.17.2 bus collision during a repeated start condition during a repeated start condition, a bus collision occurs if: a) a low level is sampled on sda when scl goes from low level to high level. b) scl goes low before sda is asserted low, indicating that another master is attempting to transmit a data ? 1 ?. when the user deasserts sda and the pin is allowed to float high, the brg is loaded with sspadd and counts down to 0. the scl pin is then deasserted and when sampled high, the sda pin is sampled. if sda is low, a bus collision has occurred (i.e., another master is attempting to transmit a data ? 0 ?, figure 15-29 ). if sda is sampled high, the brg is reloaded and begins counting. if sda goes from high-to-low before the brg times out, no bus collision occurs because no two masters can assert sda at exactly the same time. if scl goes from high-to-low before the brg times out and sda has not already been asserted, a bus collision occurs. in this case, another master is attempting to transmit a data ? 1 ? during the repeated start condition, see figure 15-30 . if, at the end of the brg time-out, both scl and sda are still high, the sda pin is driven low and the brg is reloaded and begins counting. at the end of the count, regardless of the status of the scl pin, the scl pin is driven low and the repeated start condition is complete. figure 15-29: bus collision during a repeat ed start condition (case 1) figure 15-30: bus collision during repeat ed start condition (case 2) sda scl rsen bclif s sspif sample sda when scl goes high. if sda = 0 , set bclif and release sda and scl. cleared by software ? 0 ? ? 0 ? sda scl bclif rsen s sspif interrupt cleared by software scl goes low before sda, set bclif. release sda and scl. t brg t brg ? 0 ?
? 2010 microchip technology inc. preliminary ds41350e-page 179 pic18f/lf1xk50 15.3.17.3 bus collision during a stop condition bus collision occurs during a stop condition if: a) after the sda pin has been deasserted and allowed to float high, sda is sampled low after the brg has timed out. b) after the scl pin is deasserted, scl is sampled low before sda goes high. the stop condition begins with sda asserted low. when sda is sampled low, the scl pin is allowed to float. when the pin is sampled high (clock arbitration), the baud rate generator is loaded with sspadd and counts down to 0. after the brg times out, sda is sampled. if sda is sampled low, a bus collision has occurred. this is due to another master attempting to drive a data ? 0 ? ( figure 15-31 ). if the scl pin is sampled low before sda is allowed to float high, a bus collision occurs. this is another case of another master attempting to drive a data ? 0 ? ( figure 15-32 ). figure 15-31: bus collision during a stop condition (case 1) figure 15-32: bus collision during a stop condition (case 2) sda scl bclif pen p sspif t brg t brg t brg sda asserted low sda sampled low after t brg , set bclif ? 0 ? ? 0 ? sda scl bclif pen p sspif t brg t brg t brg assert sda scl goes low before sda goes high, set bclif ? 0 ? ? 0 ?
pic18f/lf1xk50 ds41350e-page 180 preliminary ? 2010 microchip technology inc. table 15-4: summary of registers associated with i 2 c? name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page ipr1 ? adip rcip txip sspip ccp1ip tmr2ip tmr1ip 288 pir1 ? adif rcif txif sspif ccp1if tmr2if tmr1if 288 pie1 ? adie rcie txie sspie ccp1ie tmr2ie tmr1ie 288 ipr2 oscfip c1ip c2ip eeip bclip usbip tmr3ip ? 288 pir2 oscfif c1if c2if eeif bclif usbif tmr3if ? 288 pie2 oscfie c1ie c2ie eeie bclie usbie tmr3ie ? 288 sspadd ssp address register in i 2 c? slave mode. ssp baud rate reload register in i 2 c master mode. 286 sspbuf ssp receive buffer/transmit register 286 sspcon1 wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 286 sspcon2 gcen ackstat ackdt acken rcen pen rsen sen 286 sspmsk msk7 msk6 msk5 msk4 msk3 msk2 msk1 msk0 288 sspstat smp cke d/a psr/w ua bf 286 trisb trisb7 trisb6 trisb5 trisb4 ? ? ? ? 288 legend: ? = unimplemented, read as ?0?. shaded cells are not used by i 2 c?.
? 2010 microchip technology inc. preliminary ds41350e-page 181 pic18f/lf1xk50 16.0 enhanced universal synchronous asynchronous receiver transmitter (eusart) the enhanced universal synchronous asynchronous receiver transmitter (eusart) module is a serial i/o communications peripheral. it contains all the clock generators, shift registers and data buffers necessary to perform an input or output serial data transfer independent of device program execution. the eusart, also known as a serial communications interface (sci), can be configured as a full-duplex asynchronous system or half-duplex synchronous system. full-duplex mode is useful for communications with peripheral systems, such as crt terminals and personal computers. half-duplex synchronous mode is intended for communications with peripheral devices, such as a/d or d/a integrated circuits, serial eeproms or other microcontrollers. these devices typically do not have internal clocks for baud rate generation and require the external clock signal provided by a master synchronous device. the eusart module includes the following capabilities: ? full-duplex asynchronous transmit and receive ? two-character input buffer ? one-character output buffer ? programmable 8-bit or 9-bit character length ? address detection in 9-bit mode ? input buffer overrun error detection ? received character framing error detection ? half-duplex synchronous master ? half-duplex synchronous slave ? programmable clock and data polarity the eusart module implements the following additional features, making it ideally suited for use in local interconnect network (lin) bus systems: ? automatic detection and calibration of the baud rate ? wake-up on break reception ? 13-bit break character transmit block diagrams of the eusart transmitter and receiver are shown in figure 16-1 and figure 16-2 . figure 16-1: eusart transmi t block diagram txif txie interrupt txen tx9d msb lsb data bus txreg register transmit shift register (tsr) (8) 0 tx9 trmt spen tx/ck pin pin buffer and control 8 spbrg spbrgh brg16 f osc n n + 1 multiplier x4 x16 x64 sync 1x00 0 brgh x110 0 brg16 x101 0 baud rate generator ???
pic18f/lf1xk50 ds41350e-page 182 preliminary ? 2010 microchip technology inc. figure 16-2: eusart receiv e block diagram the operation of the eusart module is controlled through three registers: ? transmit status and control (txsta) ? receive status and control (rcsta) ? baud rate control (baudctl) these registers are detailed in register 16-1 , register 16-2 and register 16-3 , respectively. for all modes of eusart operation, the tris control bits corresponding to the rx/dt and tx/ck pins should be set to ? 1 ?. the eusart control will automatically reconfigure the pin from input to output, as needed. rx/dt pin pin buffer and control spen data recovery cren oerr ferr rsr register msb lsb rx9d rcreg register fifo interrupt rcif rcie data bus 8 stop start (8) 7 10 rx9 ? ? ? spbrg spbrgh brg16 rcidl f osc n n + 1 multiplier x4 x16 x64 sync 1x00 0 brgh x110 0 brg16 x101 0 baud rate generator
? 2010 microchip technology inc. preliminary ds41350e-page 183 pic18f/lf1xk50 16.1 eusart asynchronous mode the eusart transmits and receives data using the standard non-return-to-zero (nrz) format. nrz is implemented with two levels: a v oh mark state which represents a ? 1 ? data bit, and a v ol space state which represents a ? 0 ? data bit. nrz refers to the fact that consecutively transmitted data bits of the same value stay at the output level of that bit without returning to a neutral level between each bit transmission. an nrz transmission port idles in the mark state. each character transmission consists of one start bit followed by eight or nine data bits and is always terminated by one or more stop bits. the start bit is always a space and the stop bits are always marks. the most common data format is 8 bits. each transmitted bit persists for a period of 1/(baud rate). an on-chip dedicated 8-bit/16-bit baud rate generator is used to derive standard baud rate frequencies from the system oscillator. see table 16-5 for examples of baud rate configurations. the eusart transmits and receives the lsb first. the eusart?s transmitter and receiver are functionally independent, but share the same data format and baud rate. parity is not supported by the hardware, but can be implemented in software and stored as the ninth data bit. 16.1.1 eusart asynchronous transmitter the eusart transmitter block diagram is shown in figure 16-1 . the heart of the transmitter is the serial transmit shift register (tsr), which is not directly accessible by software. the tsr obtains its data from the transmit buffer, which is the txreg register. 16.1.1.1 enabling the transmitter the eusart transmitter is enabled for asynchronous operations by configuring the following three control bits: ?txen = 1 ? sync = 0 ? spen = 1 all other eusart control bits are assumed to be in their default state. setting the txen bit of the txsta register enables the transmitter circuitry of the eusart. clearing the sync bit of the txsta register configures the eusart for asynchronous operation. setting the spen bit of the rcsta register enables the eusart and automatically configures the tx/ck i/o pin as an output. if the tx/ck pin is shared with an analog peripheral the analog i/o function must be disabled by clearing the corresponding ansel bit. 16.1.1.2 transmitting data a transmission is initiated by writing a character to the txreg register. if this is the first character, or the previous character has been completely flushed from the tsr, the data in the txreg is immediately transferred to the tsr register. if the tsr still contains all or part of a previous character, the new character data is held in the txreg until the stop bit of the previous character has been transmitted. the pending character in the txreg is then transferred to the tsr in one t cy immediately following the stop bit transmission. the transmission of the start bit, data bits and stop bit sequence commences immediately following the transfer of the data to the tsr from the txreg. 16.1.1.3 transmit data polarity the polarity of the transmit data can be controlled with the cktxp bit of the baudcon register. the default state of this bit is ? 0 ? which selects high true transmit idle and data bits. setting the cktxp bit to ? 1 ? will invert the transmit data resulting in low true idle and data bits. the cktxp bit controls transmit data polarity only in asynchronous mode. in synchronous mode the cktxp bit has a different function. note 1: when the spen bit is set the rx/dt i/o pin is automatically configured as an input, regardless of the state of the correspond- ing tris bit and whether or not the eusart receiver is enabled. the rx/dt pin data can be read via a normal port read but port latch data output is pre- cluded. 2: the txif transmitter interrupt flag is set when the txen enable bit is set.
pic18f/lf1xk50 ds41350e-page 184 preliminary ? 2010 microchip technology inc. 16.1.1.4 transmit interrupt flag the txif interrupt flag bit of the pir1 register is set whenever the eusart transmitter is enabled and no character is being held for transmission in the txreg. in other words, the txif bit is only clear when the tsr is busy with a character and a new character has been queued for transmission in the txreg. the txif flag bit is not cleared immediately upon writing txreg. txif becomes valid in the second instruction cycle following the write execution. polling txif immediately following the txreg write will return invalid results. the txif bit is read-only, it cannot be set or cleared by software. the txif interrupt can be enabled by setting the txie interrupt enable bit of the pie1 register. however, the txif flag bit will be set whenever the txreg is empty, regardless of the state of txie enable bit. to use interrupts when transmitting data, set the txie bit only when there is more data to send. clear the txie interrupt enable bit upon writing the last character of the transmission to the txreg. 16.1.1.5 tsr status the trmt bit of the txsta register indicates the status of the tsr register. this is a read-only bit. the trmt bit is set when the tsr register is empty and is cleared when a character is transferred to the tsr register from the txreg. the trmt bit remains clear until all bits have been shifted out of the tsr register. no interrupt logic is tied to this bit, so the user needs to poll this bit to determine the tsr status. 16.1.1.6 transmitting 9-bit characters the eusart supports 9-bit character transmissions. when the tx9 bit of the txsta register is set the eusart will shift 9 bits out for each character transmit- ted. the tx9d bit of the txsta register is the ninth, and most significant, data bit. when transmitting 9-bit data, the tx9d data bit must be written before writing the 8 least significant bits into the txreg. all nine bits of data will be transferred to the tsr shift register immediately after the txreg is written. a special 9-bit address mode is available for use with multiple receivers. see section 16.1.2.8 ?address detection? for more information on the address mode. 16.1.1.7 asynchronous transmission set-up: 1. initialize the spbrgh:spbrg register pair and the brgh and brg16 bits to achieve the desired baud rate (see section 16.3 ?eusart baud rate generator (brg)? ). 2. enable the asynchronous serial port by clearing the sync bit and setting the spen bit. 3. if 9-bit transmission is desired, set the tx9 con- trol bit. a set ninth data bit will indicate that the 8 least significant data bits are an address when the receiver is set for address detection. 4. set the cktxp control bit if inverted transmit data polarity is desired. 5. enable the transmission by setting the txen control bit. this will cause the txif interrupt bit to be set. 6. if interrupts are desired, set the txie interrupt enable bit. an interrupt will occur immediately provided that the gie and peie bits of the intcon register are also set. 7. if 9-bit transmission is selected, the ninth bit should be loaded into the tx9d data bit. 8. load 8-bit data into the txreg register. this will start the transmission. note: the tsr register is not mapped in data memory, so it is not available to the user.
? 2010 microchip technology inc. preliminary ds41350e-page 185 pic18f/lf1xk50 figure 16-3: asynchronous transmission figure 16-4: asynchronous transmiss ion (back-to-back) table 16-1: registers associated wi th asynchronous transmission name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 285 pir1 ? adif rcif txif sspif ccp1if tmr2if tmr1if 288 pie1 ? adie rcie txie sspie ccp1ie tmr2ie tmr1ie 288 ipr1 ? adip rcip txip sspip ccp1ip tmr2ip tmr1ip 288 rcsta spen rx9 sren cren adden ferr oerr rx9d 287 txreg eusart transmit register 287 txsta csrc tx9 txen sync sendb brgh trmt tx9d 287 baudcon abdovf rcidl dtrxp cktxp brg16 ? wue abden 287 spbrgh eusart baud rate generator register, high byte 287 spbrg eusart baud rate generator register, low byte 287 legend: ? = unimplemented locations read as ? 0 ?. shaded cells are not used for asynchronous transmission. word 1 stop bit word 1 transmit shift reg start bit bit 0 bit 1 bit 7/8 write to txreg word 1 brg output (shift clock) rb7/tx/ck txif bit (transmit buffer reg. empty flag) trmt bit (transmit shift reg. empty flag) 1 t cy pin transmit shift reg write to txreg brg output (shift clock) rb7/tx/ck txif bit (interrupt reg. flag) trmt bit (transmit shift reg. empty flag) word 1 word 2 word 1 word 2 start bit stop bit start bit transmit shift reg word 1 word 2 bit 0 bit 1 bit 7/8 bit 0 note: this timing diagram shows two consecutive transmissions. 1 t cy 1 t cy pin
pic18f/lf1xk50 ds41350e-page 186 preliminary ? 2010 microchip technology inc. 16.1.2 eusart asynchronous receiver the asynchronous mode would typically be used in rs-232 systems. the receiver block diagram is shown in figure 16-2 . the data is received on the rx/dt pin and drives the data recovery block. the data recovery block is actually a high-speed shifter operating at 16 times the baud rate, whereas the serial receive shift register (rsr) operates at the bit rate. when all 8 or 9 bits of the character have been shifted in, they are immediately transferred to a two character first-in-first-out (fifo) memory. the fifo buffering allows reception of two complete characters and the start of a third character before software must start servicing the eusart receiver. the fifo and rsr registers are not directly accessible by software. access to the received data is via the rcreg register. 16.1.2.1 enabling the receiver the eusart receiver is enabled for asynchronous operation by configuring the following three control bits: ? cren = 1 ? sync = 0 ? spen = 1 all other eusart control bits are assumed to be in their default state. setting the cren bit of the rcsta register enables the receiver circuitry of the eusart. clearing the sync bit of the txsta register configures the eusart for asynchronous operation. setting the spen bit of the rcsta register enables the eusart. the rx/dt i/o pin must be configured as an input by setting the corresponding tris control bit. if the rx/dt pin is shared with an analog peripheral the analog i/o function must be disabled by clearing the corresponding ansel bit. 16.1.2.2 receiving data the receiver data recovery circuit initiates character reception on the falling edge of the first bit. the first bit, also known as the start bit, is always a zero. the data recovery circuit counts one-half bit time to the center of the start bit and verifies that the bit is still a zero. if it is not a zero then the data recovery circuit aborts character reception, without generating an error, and resumes looking for the falling edge of the start bit. if the start bit zero verification succeeds then the data recovery circuit counts a full bit time to the center of the next bit. the bit is then sampled by a majority detect circuit and the resulting ? 0 ? or ? 1 ? is shifted into the rsr. this repeats until all data bits have been sampled and shifted into the rsr. one final bit time is measured and the level sampled. this is the stop bit, which is always a ? 1 ?. if the data recovery circuit samples a ? 0 ? in the stop bit position then a framing error is set for this character, otherwise the framing error is cleared for this character. see section 16.1.2.5 ?receive framing error? for more information on framing errors. immediately after all data bits and the stop bit have been received, the character in the rsr is transferred to the eusart receive fifo and the rcif interrupt flag bit of the pir1 register is set. the top character in the fifo is transferred out of the fifo by reading the rcreg register. 16.1.2.3 receive data polarity the polarity of the receive data can be controlled with the dtrxp bit of the baudcon register. the default state of this bit is ? 0 ? which selects high true receive idle and data bits. setting the dtrxp bit to ? 1 ? will invert the receive data resulting in low true idle and data bits. the dtrxp bit controls receive data polarity only in asynchronous mode. in synchronous mode the dtrxp bit has a different function. note: when the spen bit is set the tx/ck i/o pin is automatically configured as an output, regardless of the state of the corresponding tris bit and whether or not the eusart transmitter is enabled. the port latch is disconnected from the output driver so it is not possible to use the tx/ck pin as a general purpose output. note: if the receive fifo is overrun, no additional characters will be received until the overrun condition is cleared. see section 16.1.2.6 ?receive overrun error? for more information on overrun errors.
? 2010 microchip technology inc. preliminary ds41350e-page 187 pic18f/lf1xk50 16.1.2.4 receive interrupts the rcif interrupt flag bit of the pir1 register is set whenever the eusart receiver is enabled and there is an unread character in the receive fifo. the rcif interrupt flag bit is read-only, it cannot be set or cleared by software. rcif interrupts are enabled by setting the following bits: ? rcie interrupt enable bit of the pie1 register ? peie peripheral interrupt enable bit of the intcon register ? gie global interrupt enable bit of the intcon register the rcif interrupt flag bit will be set when there is an unread character in the fifo, regardless of the state of interrupt enable bits. 16.1.2.5 receive framing error each character in the receive fifo buffer has a corresponding framing error status bit. a framing error indicates that a stop bit was not seen at the expected time. the framing error status is accessed via the ferr bit of the rcsta register. the ferr bit represents the status of the top unread character in the receive fifo. therefore, the ferr bit must be read before reading the rcreg. the ferr bit is read-only and only applies to the top unread character in the receive fifo. a framing error (ferr = 1 ) does not preclude reception of additional characters. it is not necessary to clear the ferr bit. reading the next character from the fifo buffer will advance the fifo to the next character and the next corresponding framing error. the ferr bit can be forced clear by clearing the spen bit of the rcsta register which resets the eusart. clearing the cren bit of the rcsta register does not affect the ferr bit. a framing error by itself does not generate an interrupt. 16.1.2.6 receive overrun error the receive fifo buffer can hold two characters. an overrun error will be generated if a third character, in its entirety, is received before the fifo is accessed. when this happens the oerr bit of the rcsta register is set. the characters already in the fifo buffer can be read but no additional characters will be received until the error is cleared. the error must be cleared by either clearing the cren bit of the rcsta register or by resetting the eusart by clearing the spen bit of the rcsta register. 16.1.2.7 receiving 9-bit characters the eusart supports 9-bit character reception. when the rx9 bit of the rcsta register is set, the eusart will shift 9 bits into the rsr for each character received. the rx9d bit of the rcsta register is the ninth and most significant data bit of the top unread character in the receive fifo. when reading 9-bit data from the receive fifo buffer, the rx9d data bit must be read before reading the 8 least significant bits from the rcreg. 16.1.2.8 address detection a special address detection mode is available for use when multiple receivers share the same transmission line, such as in rs-485 systems. address detection is enabled by setting the adden bit of the rcsta register. address detection requires 9-bit character reception. when address detection is enabled, only characters with the ninth data bit set will be transferred to the receive fifo buffer, thereby setting the rcif interrupt bit. all other characters will be ignored. upon receiving an address character, user software determines if the address matches its own. upon address match, user software must disable address detection by clearing the adden bit before the next stop bit occurs. when user software detects the end of the message, determined by the message protocol used, software places the receiver back into the address detection mode by setting the adden bit. note: if all receive characters in the receive fifo have framing errors, repeated reads of the rcreg will not clear the ferr bit.
pic18f/lf1xk50 ds41350e-page 188 preliminary ? 2010 microchip technology inc. 16.1.2.9 asynchronous reception set-up: 1. initialize the spbrgh:spbrg register pair and the brgh and brg16 bits to achieve the desired baud rate (see section 16.3 ?eusart baud rate generator (brg)? ). 2. enable the serial port by setting the spen bit and the rx/dt pin tris bit. the sync bit must be clear for asynchronous operation. 3. if interrupts are desired, set the rcie interrupt enable bit and set the gie and peie bits of the intcon register. 4. if 9-bit reception is desired, set the rx9 bit. 5. set the dtrxp if inverted receive polarity is desired. 6. enable reception by setting the cren bit. 7. the rcif interrupt flag bit will be set when a character is transferred from the rsr to the receive buffer. an interrupt will be generated if the rcie interrupt enable bit was also set. 8. read the rcsta register to get the error flags and, if 9-bit data reception is enabled, the ninth data bit. 9. get the received 8 least significant data bits from the receive buffer by reading the rcreg register. 10. if an overrun occurred, clear the oerr flag by clearing the cren receiver enable bit. 16.1.2.10 9-bit address detection mode set-up this mode would typically be used in rs-485 systems. to set up an asynchronous reception with address detect enable: 1. initialize the spbrgh, spbrg register pair and the brgh and brg16 bits to achieve the desired baud rate (see section 16.3 ?eusart baud rate generator (brg)? ). 2. enable the serial port by setting the spen bit. the sync bit must be clear for asynchronous operation. 3. if interrupts are desired, set the rcie interrupt enable bit and set the gie and peie bits of the intcon register. 4. enable 9-bit reception by setting the rx9 bit. 5. enable address detection by setting the adden bit. 6. set the dtrxp if inverted receive polarity is desired. 7. enable reception by setting the cren bit. 8. the rcif interrupt flag bit will be set when a character with the ninth bit set is transferred from the rsr to the receive buffer. an interrupt will be generated if the rcie interrupt enable bit was also set. 9. read the rcsta register to get the error flags. the ninth data bit will always be set. 10. get the received 8 least significant data bits from the receive buffer by reading the rcreg register. software determines if this is the device?s address. 11. if an overrun occurred, clear the oerr flag by clearing the cren receiver enable bit. 12. if the device has been addressed, clear the adden bit to allow all received data into the receive buffer and generate interrupts. figure 16-5: asynchronous reception start bit bit 7/8 bit 1 bit 0 bit 7/8 bit 0 stop bit start bit start bit bit 7/8 stop bit rx/dt pin reg rcv buffer reg rcv shift read rcv buffer reg rcreg rcif (interrupt flag) oerr bit cren word 1 rcreg word 2 rcreg stop bit note: this timing diagram shows three words appearing on the rx input. the rcreg (receive buffer) is read after the third word, causing the oerr (overrun) bit to be set. rcidl
? 2010 microchip technology inc. preliminary ds41350e-page 189 pic18f/lf1xk50 table 16-2: registers associated with asynchronous reception name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 285 pir1 ? adif rcif txif sspif ccp1if tmr2if tmr1if 288 pie1 ? adie rcie txie sspie ccp1ie tmr2ie tmr1ie 288 ipr1 ? adip rcip txip sspip ccp1ip tmr2ip tmr1ip 288 rcsta spen rx9 sren cren adden ferr oerr rx9d 287 rcreg eusart receive register 287 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 288 txsta csrc tx9 txen sync sendb brgh trmt tx9d 287 baudcon abdovf rcidl dtrxp cktxp brg16 ? wue abden 287 spbrgh eusart baud rate generator register, high byte 287 spbrg eusart baud rate generator register, low byte 287 legend: ? = unimplemented locations read as ? 0 ?. shaded cells are not used for asynchronous reception.
pic18f/lf1xk50 ds41350e-page 190 preliminary ? 2010 microchip technology inc. 16.2 clock accuracy with asynchronous operation the factory calibrates the internal oscillator block out- put (hfintosc). however, the hfintosc frequency may drift as v dd or temperature changes, and this directly affects the asynchronous baud rate. two meth- ods may be used to adjust the baud rate clock, but both require a reference clock source of some kind. the first (preferred) method uses the osctune register to adjust the hfintosc output. adjusting the value in the osctune register allows for fine resolution changes to the system clock source. see section 2.6.1 ?osctune register? for more information. the other method adjusts the value in the baud rate generator. this can be done automatically with the auto-baud detect feature (see section 16.3.1 ?auto-baud detect? ). there may not be fine enough resolution when adjusting the baud rate generator to compensate for a gradual change in the peripheral clock frequency. register 16-1: txsta: transmit status and control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r-1 r/w-0 csrc tx9 txen (1) sync sendb brgh trmt tx9d bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 csrc: clock source select bit asynchronous mode : don?t care synchronous mode : 1 = master mode (clock generated internally from brg) 0 = slave mode (clock from external source) bit 6 tx9: 9-bit transmit enable bit 1 = selects 9-bit transmission 0 = selects 8-bit transmission bit 5 txen: transmit enable bit (1) 1 = transmit enabled 0 = transmit disabled bit 4 sync: eusart mode select bit 1 = synchronous mode 0 = asynchronous mode bit 3 sendb: send break character bit asynchronous mode : 1 = send sync break on next transmission (cleared by hardware upon completion) 0 = sync break transmission completed synchronous mode : don?t care bit 2 brgh: high baud rate select bit asynchronous mode : 1 = high speed 0 = low speed synchronous mode: unused in this mode bit 1 trmt: transmit shift register status bit 1 = tsr empty 0 = tsr full bit 0 tx9d: ninth bit of transmit data can be address/data bit or a parity bit. note 1: sren/cren overrides txen in sync mode.
? 2010 microchip technology inc. preliminary ds41350e-page 191 pic18f/lf1xk50 register 16-2: rcsta: receive status and control register (1) r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r-0 r-0 r-x spen rx9 sren cren adden ferr oerr rx9d bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 spen: serial port enable bit 1 = serial port enabled (configures rx/dt and tx/ck pins as serial port pins) 0 = serial port disabled (held in reset) bit 6 rx9: 9-bit receive enable bit 1 = selects 9-bit reception 0 = selects 8-bit reception bit 5 sren: single receive enable bit asynchronous mode : don?t care synchronous mode ? master : 1 = enables single receive 0 = disables single receive this bit is cleared after reception is complete. synchronous mode ? slave don?t care bit 4 cren: continuous receive enable bit asynchronous mode : 1 = enables receiver 0 = disables receiver synchronous mode : 1 = enables continuous receive until enable bit cren is cleared (cren overrides sren) 0 = disables continuous receive bit 3 adden: address detect enable bit asynchronous mode 9-bit (rx9 = 1 ) : 1 = enables address detection, enable interrupt and load the receive buffer when rsr<8> is set 0 = disables address detection, all bytes are received and ninth bit can be used as parity bit asynchronous mode 8-bit (rx9 = 0 ) : don?t care bit 2 ferr: framing error bit 1 = framing error (can be updated by reading rcreg register and receive next valid byte) 0 = no framing error bit 1 oerr: overrun error bit 1 = overrun error (can be cleared by clearing bit cren) 0 = no overrun error bit 0 rx9d: ninth bit of received data this can be address/data bit or a parity bit and must be calculated by user firmware.
pic18f/lf1xk50 ds41350e-page 192 preliminary ? 2010 microchip technology inc. register 16-3: baudcon: baud rate control register r-0 r-1 r/w-0 r/w-0 r/w-0 u-0 r/w-0 r/w-0 abdovf rcidl dtrxp cktxp brg16 ? wue abden bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 abdovf: auto-baud detect overflow bit asynchronous mode : 1 = auto-baud timer overflowed 0 = auto-baud timer did not overflow synchronous mode : don?t care bit 6 rcidl : receive idle flag bit asynchronous mode : 1 = receiver is idle 0 = start bit has been detected and the receiver is active synchronous mode : don?t care bit 5 dtrxp : data/receive polarity select bit asynchronous mode : 1 = receive data (rx) is inverted (active-low) 0 = receive data (rx) is not inverted (active-high) synchronous mode : 1 = data (dt) is inverted (active-low) 0 = data (dt) is not inverted (active-high) bit 4 cktxp : clock/transmit polarity select bit asynchronous mode : 1 = idle state for transmit (tx) is low 0 = idle state for transmit (tx) is high synchronous mode : 1 = data changes on the falling edge of the clock and is sampled on the rising edge of the clock 0 = data changes on the rising edge of the clock and is sampled on the falling edge of the clock bit 3 brg16: 16-bit baud rate generator bit 1 = 16-bit baud rate generator is used (spbrgh:spbrg) 0 = 8-bit baud rate generator is used (spbrg) bit 2 unimplemented: read as ? 0 ? bit 1 wue: wake-up enable bit asynchronous mode : 1 = receiver is waiting for a falling edge. no character will be received but rcif will be set on the falling edge. wue will automatically clear on the rising edge. 0 = receiver is operating normally synchronous mode : don?t care bit 0 abden : auto-baud detect enable bit asynchronous mode : 1 = auto-baud detect mode is enabled (clears when auto-baud is complete) 0 = auto-baud detect mode is disabled synchronous mode : don?t care
? 2010 microchip technology inc. preliminary ds41350e-page 193 pic18f/lf1xk50 16.3 eusart baud rate generator (brg) the baud rate generator (brg) is an 8-bit or 16-bit timer that is dedicated to the support of both the asynchronous and synchronous eusart operation. by default, the brg operates in 8-bit mode. setting the brg16 bit of the baudcon register selects 16-bit mode. the spbrgh:spbrg register pair determines the period of the free running baud rate timer. in asynchronous mode the multiplier of the baud rate period is determined by both the brgh bit of the txsta register and the brg16 bit of the baudcon register. in synchronous mode, the brgh bit is ignored. table 16-3 contains the formulas for determining the baud rate. example 16-1 provides a sample calculation for determining the baud rate and baud rate error. typical baud rates and error values for various asynchronous modes have been computed for your convenience and are shown in table 16-5 . it may be advantageous to use the high baud rate (brgh = 1 ), or the 16-bit brg (brg16 = 1 ) to reduce the baud rate error. the 16-bit brg mode is used to achieve slow baud rates for fast oscillator frequencies. writing a new value to the spbrgh, spbrg register pair causes the brg timer to be reset (or cleared). this ensures that the brg does not wait for a timer overflow before outputting the new baud rate. if the system clock is changed during an active receive operation, a receive error or data loss may result. to avoid this problem, check the status of the rcidl bit to make sure that the receive operation is idle before changing the system clock. example 16-1: calculating baud rate error table 16-3: baud rate formulas table 16-4: registers associated with baud rate generator for a device with f osc of 16 mhz, desired baud rate of 9600, asynchronous mode, 8-bit brg: solving for spbrgh:spbrg: desired baud rate f osc 64 [spbrgh:spbrg] 1 + ?? -------------------------------------------------------------------- - = 25.042 ?? 25 == calculated baud rate 16000000 64 25 1 + ?? -------------------------- - = 9615 = error calc. baud rate desired baud rate ? desired baud rate -------------------------------------------------------------------------------------------- = 9615 9600 ? ?? 9600 ---------------------------------- 0 . 1 6 % == f osc x = 64 * (desired baud rate ) -1 ( ) 16,000,000 = 64 * 9600 -1 ( ) configuration bits brg/eusart mode baud rate formula sync brg16 brgh 000 8-bit/asynchronous f osc /[64 (n+1)] 001 8-bit/asynchronous f osc /[16 (n+1)] 010 16-bit/asynchronous 011 16-bit/asynchronous f osc /[4 (n+1)] 10x 8-bit/synchronous 11x 16-bit/synchronous legend: x = don?t care, n = value of spbrgh, spbrg register pair name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page txsta csrc tx9 txen sync sendb brgh trmt tx9d 287 rcsta spen rx9 sren cren adden ferr oerr rx9d 287 baudcon abdovf rcidl dtrxp cktxp brg16 ? wue abden 287 spbrgh eusart baud rate generator register, high byte 287 spbrg eusart baud rate generator register, low byte 287 legend: ? = unimplemented, read as ? 0 ?. shaded cells are not used by the brg.
pic18f/lf1xk50 ds41350e-page 194 preliminary ? 2010 microchip technology inc. table 16-5: baud rates for asynchronous modes baud rate sync = 0 , brgh = 0 , brg16 = 0 f osc = 48.000 mhz f osc = 18.432 mhz f osc = 12.000 mhz f osc = 11.0592 mhz actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) 300?? ? ?? ? ?? ? ?? ? 1200 ?? ? 1200 0.00 239 1202 0.16 155 1200 0.00 143 2400 ?? ? 2400 0.00 119 2404 0.16 77 2400 0.00 71 9600 9615 0.16 77 9600 0.00 29 9375 -2.34 19 9600 0.00 17 10417 10417 0.00 71 10286 -1.26 27 10417 0.00 17 10165 -2.42 16 19.2k 19.23k 0.16 38 19.20k 0.00 14 18.75k -2.34 9 19.20k 0.00 8 57.6k 57.69k 0.16 12 57.60k 0.00 7 ?? ? 57.60k 0.00 2 115.2k ? ? ? ? ? ? ? ? ? ? ? ? baud rate sync = 0 , brgh = 0 , brg16 = 0 f osc = 8.000 mhz f osc = 4.000 mhz f osc = 3.6864 mhz f osc = 1.000 mhz actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) 300 ? ? ? 300 0.16 207 300 0.00 191 300 0.16 51 1200 1202 0.16 103 1202 0.16 51 1200 0.00 47 1202 0.16 12 2400 2404 0.16 51 2404 0.16 25 2400 0.00 23 ? ? ? 9600 9615 0.16 12 ? ? ? 9600 0.00 5 ? ? ? 10417 10417 0.00 11 10417 0.00 5 ? ? ? ? ? ? 19.2k ? ? ? ? ? ? 19.20k 0.00 2 ? ? ? 57.6k ? ? ? ? ? ? 57.60k 0.00 0 ? ? ? 115.2k ? ? ? ? ? ? ? ? ? ? ? ? baud rate sync = 0 , brgh = 1 , brg16 = 0 f osc = 48.000 mhz f osc = 18.432 mhz f osc = 12.000 mhz f osc = 11.0592 mhz actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) 300 ?? ? ?? ? ?? ? ?? ? 1200 ? ? ? ? ? ? ? ? ? ? ? ? 2400 ? ? ? ? ? ? ?? ? ?? ? 9600 ? ? ? 9600 0.00 119 9615 0.16 77 9600 0.00 71 10417 ? ? ? 10378 -0.37 110 10417 0.00 71 10473 0.53 65 19.2k 19.23k 0.16 155 19.20k 0.00 59 19.23k 0.16 38 19.20k 0.00 35 57.6k 57.69k 0.16 51 57.60k 0.00 19 57.69k 0.16 12 57.60k 0.00 11 115.2k 115.38k 0.16 25 115.2k 0.00 9 ? ? ? 115.2k 0.00 5
? 2010 microchip technology inc. preliminary ds41350e-page 195 pic18f/lf1xk50 baud rate sync = 0 , brgh = 1 , brg16 = 0 f osc = 8.000 mhz f osc = 4.000 mhz f osc = 3.6864 mhz f osc = 1.000 mhz actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) actual rate % error spbrg value (decimal) 300 ?? ? ? ? ? ? ? ? 300 0.16 207 1200 ? ? ? 1202 0.16 207 1200 0.00 191 1202 0.16 51 2400 2404 0.16 207 2404 0.16 103 2400 0.00 95 2404 0.16 25 9600 9615 0.16 51 9615 0.16 25 9600 0.00 23 ? ? ? 10417 10417 0.00 47 10417 0.00 23 10473 0.53 21 10417 0.00 5 19.2k 19231 0.16 25 19.23k 0.16 12 19.2k 0.00 11 ? ? ? 57.6k 55556 -3.55 8 ? ? ? 57.60k 0.00 3 ? ? ? 115.2k ? ? ? ? ? ? 115.2k 0.00 1 ? ? ? baud rate sync = 0 , brgh = 0 , brg16 = 1 f osc = 48.000 mhz f osc = 18.432 mhz f osc = 12.000 mhz f osc = 11.0592 mhz actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) 300 300.0 0.00 9999 300.0 0.00 3839 300 0.00 2499 300.0 0.00 2303 1200 1200.1 0.00 2499 1200 0.00 959 1200 0.00 624 1200 0.00 575 2400 2400 0.00 1249 2400 0.00 479 2404 0.16 311 2400 0.00 287 9600 9615 0.16 311 9600 0.00 119 9615 0.16 77 9600 0.00 71 10417 10417 0.00 287 10378 -0.37 110 10417 0.00 71 10473 0.53 65 19.2k 19.23k 0.16 155 19.20k 0.00 59 19.23k 0.16 38 19.20k 0.00 35 57.6k 57.69k 0.16 51 57.60k 0.00 19 57.69k 0.16 12 57.60k 0.00 11 115.2k 115.38k 0.16 25 115.2k 0.00 9 ? ? ? 115.2k 0.00 5 baud rate sync = 0 , brgh = 0 , brg16 = 1 f osc = 8.000 mhz f osc = 4.000 mhz f osc = 3.6864 mhz f osc = 1.000 mhz actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) 300 299.9 -0.02 1666 300.1 0.04 832 300.0 0.00 767 300.5 0.16 207 1200 1199 -0.08 416 1202 0.16 207 1200 0.00 191 1202 0.16 51 2400 2404 0.16 207 2404 0.16 103 2400 0.00 95 2404 0.16 25 9600 9615 0.16 51 9615 0.16 25 9600 0.00 23 ? ? ? 10417 10417 0.00 47 10417 0.00 23 10473 0.53 21 10417 0.00 5 19.2k 19.23k 0.16 25 19.23k 0.16 12 19.20k 0.00 11 ? ? ? 57.6k 55556 -3.55 8 ? ? ? 57.60k 0.00 3 ? ? ? 115.2k ? ? ? ? ? ? 115.2k 0.00 1 ? ? ? table 16-5: baud rates for asynchronous modes (continued)
pic18f/lf1xk50 ds41350e-page 196 preliminary ? 2010 microchip technology inc. baud rate sync = 0 , brgh = 1 , brg16 = 1 or sync = 1 , brg16 = 1 f osc = 48.000 mhz f osc = 18.432 mhz f osc = 12.000 mhz f osc = 11.0592 mhz actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) 300 300 0.00 39999 300.0 0.00 15359 300 0.00 9999 300.0 0.00 9215 1200 1200 0.00 9999 1200 0.00 3839 1200 0.00 2499 1200 0.00 2303 2400 2400 0.00 4999 2400 0.00 1919 2400 0.00 1249 2400 0.00 1151 9600 9600 0.00 1249 9600 0.00 479 9615 0.16 311 9600 0.00 287 10417 10417 0.00 1151 10425 0.08 441 10417 0.00 287 10433 0.16 264 19.2k 19.20k 0.00 624 19.20k 0.00 239 19.23k 0.16 155 19.20k 0.00 143 57.6k 57.69k 0.16 207 57.60k 0.00 79 57.69k 0.16 51 57.60k 0.00 47 115.2k 115.38k 0.16 103 115.2k 0.00 39 115.38k 0.16 25 115.2k 0.00 23 baud rate sync = 0 , brgh = 1 , brg16 = 1 or sync = 1 , brg16 = 1 f osc = 8.000 mhz f osc = 4.000 mhz f osc = 3.6864 mhz f osc = 1.000 mhz actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) actual rate % error spbrgh :spbrg (decimal) 300 300.0 0.00 6666 300.0 0.01 3332 300.0 0.00 3071 300.1 0.04 832 1200 1200 -0.02 1666 1200 0.04 832 1200 0.00 767 1202 0.16 207 2400 2401 0.04 832 2398 0.08 416 2400 0.00 383 2404 0.16 103 9600 9615 0.16 207 9615 0.16 103 9600 0.00 95 9615 0.16 25 10417 10417 0.00 191 10417 0.00 95 10473 0.53 87 10417 0.00 23 19.2k 19.23k 0.16 103 19.23k 0.16 51 19.20k 0.00 47 19.23k 0.16 12 57.6k 57.14k -0.79 34 58.82k 2.12 16 57.60k 0.00 15 ? ? ? 115.2k 117.6k 2.12 16 111.1k -3.55 8 115.2k 0.00 7 ? ? ? table 16-5: baud rates for asynchronous modes (continued)
? 2010 microchip technology inc. preliminary ds41350e-page 197 pic18f/lf1xk50 16.3.1 auto-baud detect the eusart module supports automatic detection and calibration of the baud rate. in the auto-baud detect (abd) mode, the clock to the brg is reversed. rather than the brg clocking the incoming rx signal, the rx signal is timing the brg. the baud rate generator is used to time the period of a received 55h (ascii ?u?) which is the sync character for the lin bus. the unique feature of this character is that it has five rising edges including the stop bit edge. setting the abden bit of the baudcon register starts the auto-baud calibration sequence ( figure 16-6 ). while the abd sequence takes place, the eusart state machine is held in idle. on the first rising edge of the receive line, after the start bit, the spbrg begins counting up using the brg counter clock as shown in table 16-6 . the fifth rising edge will occur on the rx pin at the end of the eighth bit period. at that time, an accumulated value totaling the proper brg period is left in the spbrgh:spbrg register pair, the abden bit is automatically cleared, and the rcif interrupt flag is set. a read operation on the rcreg needs to be performed to clear the rcif interrupt. rcreg content should be discarded. when calibrating for modes that do not use the spbrgh register the user can verify that the spbrg register did not overflow by checking for 00h in the spbrgh register. the brg auto-baud clock is determined by the brg16 and brgh bits as shown in tab le 1 6- 6 . during abd, both the spbrgh and spbrg registers are used as a 16-bit counter, independent of the brg16 bit setting. while calibrating the baud rate period, the spbrgh and spbrg registers are clocked at 1/8th the brg base clock rate. the resulting byte measurement is the average bit time when clocked at full speed. table 16-6: brg counter clock rates figure 16-6: automatic baud rate calibration note 1: if the wue bit is set with the abden bit, auto-baud detection will occur on the byte following the break character (see section 16.3.3 ?auto-wake-up on break? ). 2: it is up to the user to determine that the incoming character baud rate is within the range of the selected brg clock source. some combinations of oscillator frequency and eusart baud rates are not possible. 3: during the auto-baud process, the auto-baud counter starts counting at 1. upon completion of the auto-baud sequence, to achieve maximum accuracy, subtract 1 from the spbrgh:spbrg register pair. brg16 brgh brg base clock brg abd clock 00 f osc /64 f osc /512 01 f osc /16 f osc /128 10 f osc /16 f osc /128 11 f osc /4 f osc /32 note: during the abd sequence, spbrg and spbrgh registers are both used as a 16-bit counter, independent of brg16 setting. brg value rx pin abden bit rcif bit bit 0 bit 1 (interrupt) read rcreg brg clock start auto cleared set by user xxxxh 0000h edge #1 bit 2 bit 3 edge #2 bit 4 bit 5 edge #3 bit 6 bit 7 edge #4 stop bit edge #5 001ch note 1: the abd sequence requires the eusart module to be configured in asynchronous mode. spbrg xxh 1ch spbrgh xxh 00h rcidl
pic18f/lf1xk50 ds41350e-page 198 preliminary ? 2010 microchip technology inc. 16.3.2 auto-baud overflow during the course of automatic baud detection, the abdovf bit of the baudcon register will be set if the baud rate counter overflows before the fifth rising edge is detected on the rx pin. the abdovf bit indicates that the counter has exceeded the maximum count that can fit in the 16 bits of the spbrgh:spbrg register pair. after the abdovf has been set, the counter con- tinues to count until the fifth rising edge is detected on the rx pin. upon detecting the fifth rx edge, the hard- ware will set the rcif interrupt flag and clear the abden bit of the baudcon register. the rcif flag can be subsequently cleared by reading the rcreg register. the abdovf flag of the baudcon register can be cleared by software directly. to terminate the auto-baud process before the rcif flag is set, clear the abden bit then clear the abdovf bit of the baudcon register. the abdovf bit will remain set if the abden bit is not cleared first. 16.3.3 auto-wake-up on break during sleep mode, all clocks to the eusart are suspended. because of this, the baud rate generator is inactive and a proper character reception cannot be performed. the auto-wake-up feature allows the controller to wake-up due to activity on the rx/dt line. this feature is available only in asynchronous mode. the auto-wake-up feature is enabled by setting the wue bit of the baudcon register. once set, the normal receive sequence on rx/dt is disabled, and the eusart remains in an idle state, monitoring for a wake-up event independent of the cpu mode. a wake-up event consists of a high-to-low transition on the rx/dt line. (this coincides with the start of a sync break or a wake-up signal character for the lin protocol.) the eusart module generates an rcif interrupt coincident with the wake-up event. the interrupt is generated synchronously to the q clocks in normal cpu operating modes ( figure 16-7 ), and asynchronously if the device is in sleep mode ( figure 16-8 ). the interrupt condition is cleared by reading the rcreg register. the wue bit is automatically cleared by the low-to-high transition on the rx line at the end of the break. this signals to the user that the break event is over. at this point, the eusart module is in idle mode waiting to receive the next character. 16.3.3.1 special considerations break character to avoid character errors or character fragments during a wake-up event, the wake-up character must be all zeros. when the wake-up is enabled the function works independent of the low time on the data stream. if the wue bit is set and a valid non-zero character is received, the low time from the start bit to the first rising edge will be interpreted as the wake-up event. the remaining bits in the character will be received as a fragmented character and subsequent characters can result in framing or overrun errors. therefore, the initial character in the transmission must be all ? 0 ?s. this must be 10 or more bit times, 13-bit times recommended for lin bus, or any number of bit times for standard rs-232 devices. oscillator startup time oscillator start-up time must be considered, especially in applications using oscillators with longer start-up intervals (i.e., lp, xt or hs/pll mode). the sync break (or wake-up signal) character must be of sufficient length, and be followed by a sufficient interval, to allow enough time for the selected oscillator to start and provide proper initialization of the eusart. wue bit the wake-up event causes a receive interrupt by setting the rcif bit. the wue bit is cleared by hardware by a rising edge on rx/dt. the interrupt condition is then cleared by software by reading the rcreg register and discarding its contents. to ensure that no actual data is lost, check the rcidl bit to verify that a receive operation is not in process before setting the wue bit. if a receive operation is not occurring, the wue bit may then be set just prior to entering the sleep mode.
? 2010 microchip technology inc. preliminary ds41350e-page 199 pic18f/lf1xk50 figure 16-7: auto-wake-up bit (wue) timing during no rmal operation figure 16-8: auto-wake-up bit (wue) timings during sleep q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 osc1 wue bit rx/dt line rcif bit set by user auto cleared cleared due to user read of rcreg note 1: the eusart remains in idle while the wue bit is set. q1 q2 q3 q4 q1q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 osc1 wue bit rx/dt line rcif bit set by user auto cleared cleared due to user read of rcreg sleep command executed note 1 note 1: if the wake-up event requires long oscillator warm-up time, the automatic clearing of the wue bit can occur while the stposc signal is still active. this sequence should not depend on the presence of q clocks. 2: the eusart remains in idle while the wue bit is set. sleep ends
pic18f/lf1xk50 ds41350e-page 200 preliminary ? 2010 microchip technology inc. 16.3.4 break character sequence the eusart module has the capability of sending the special break character sequences that are required by the lin bus standard. a break character consists of a start bit, followed by 12 ? 0 ? bits and a stop bit. to send a break character, set the sendb and txen bits of the txsta register. the break character trans- mission is then initiated by a write to the txreg. the value of data written to txreg will be ignored and all ? 0 ?s will be transmitted. the sendb bit is automatically reset by hardware after the corresponding stop bit is sent. this allows the user to preload the transmit fifo with the next transmit byte following the break character (typically, the sync character in the lin specification). the trmt bit of the txsta register indicates when the transmit operation is active or idle, just as it does during normal transmission. see figure 16-9 for the timing of the break character sequence. 16.3.4.1 break and sync transmit sequence the following sequence will start a message frame header made up of a break, followed by an auto-baud sync byte. this sequence is typical of a lin bus master. 1. configure the eusart for the desired mode. 2. set the txen and sendb bits to enable the break sequence. 3. load the txreg with a dummy character to initiate transmission (the value is ignored). 4. write ?55h? to txreg to load the sync character into the transmit fifo buffer. 5. after the break has been sent, the sendb bit is reset by hardware and the sync character is then transmitted. when the txreg becomes empty, as indicated by the txif, the next data byte can be written to txreg. 16.3.5 receiving a break character the enhanced eusart module can receive a break character in two ways. the first method to detect a break character uses the ferr bit of the rcsta register and the received data as indicated by rcreg. the baud rate generator is assumed to have been initialized to the expected baud rate. a break character has been received when; ? rcif bit is set ? ferr bit is set ? rcreg = 00h the second method uses the auto-wake-up feature described in section 16.3.3 ?auto-wake-up on break? . by enabling this feature, the eusart will sample the next two transitions on rx/dt, cause an rcif interrupt, and receive the next data byte followed by another interrupt. note that following a break character, the user will typically want to enable the auto-baud detect feature. for both methods, the user can set the abden bit of the baudcon register before placing the eusart in sleep mode. figure 16-9: send break character sequence write to txreg dummy write brg output (shift clock) start bit bit 0 bit 1 bit 11 stop bit break txif bit (transmit interrupt flag) tx (pin) trmt bit (transmit shift reg. empty flag) sendb (send break control bit) sendb sampled here auto cleared
? 2010 microchip technology inc. preliminary ds41350e-page 201 pic18f/lf1xk50 16.4 eusart synchronous mode synchronous serial communications are typically used in systems with a single master and one or more slaves. the master device contains the necessary circuitry for baud rate generation and supplies the clock for all devices in the system. slave devices can take advantage of the master clock by eliminating the internal clock generation circuitry. there are two signal lines in synchronous mode: a bidirectional data line and a clock line. slaves use the external clock supplied by the master to shift the serial data into and out of their respective receive and transmit shift registers. since the data line is bidirectional, synchronous operation is half-duplex only. half-duplex refers to the fact that master and slave devices can receive and transmit data but not both simultaneously. the eusart can operate as either a master or slave device. start and stop bits are not used in synchronous transmissions. 16.4.1 synchronous master mode the following bits are used to configure the eusart for synchronous master operation: ? sync = 1 ? csrc = 1 ? sren = 0 (for transmit); sren = 1 (for receive) ? cren = 0 (for transmit); cren = 1 (for receive) ? spen = 1 setting the sync bit of the txsta register configures the device for synchronous operation. setting the csrc bit of the txsta register configures the device as a master. clearing the sren and cren bits of the rcsta register ensures that the device is in the transmit mode, otherwise the device will be configured to receive. setting the spen bit of the rcsta register enables the eusart. if the rx/dt or tx/ck pins are shared with an analog peripheral the analog i/o functions must be disabled by clearing the corresponding ansel bits. the tris bits corresponding to the rx/dt and tx/ck pins should be set. 16.4.1.1 master clock synchronous data transfers use a separate clock line, which is synchronous with the data. a device configured as a master transmits the clock on the tx/ck line. the tx/ck pin output driver is automatically enabled when the eusart is configured for synchronous transmit or receive operation. serial data bits change on the leading edge to ensure they are valid at the trailing edge of each clock. one clock cycle is generated for each data bit. only as many clock cycles are generated as there are data bits. 16.4.1.2 clock polarity a clock polarity option is provided for microwire compatibility. clock polarity is selected with the cktxp bit of the baudcon register. setting the cktxp bit sets the clock idle state as high. when the cktxp bit is set, the data changes on the falling edge of each clock and is sampled on the rising edge of each clock. clearing the cktxp bit sets the idle state as low. when the cktxp bit is cleared, the data changes on the rising edge of each clock and is sampled on the falling edge of each clock. 16.4.1.3 synchronous master transmission data is transferred out of the device on the rx/dt pin. the rx/dt and tx/ck pin output drivers are automat- ically enabled when the eusart is configured for synchronous master transmit operation. a transmission is initiated by writing a character to the txreg register. if the tsr still contains all or part of a previous character the new character data is held in the txreg until the last bit of the previous character has been transmitted. if this is the first character, or the pre- vious character has been completely flushed from the tsr, the data in the txreg is immediately transferred to the tsr. the transmission of the character com- mences immediately following the transfer of the data to the tsr from the txreg. each data bit changes on the leading edge of the mas- ter clock and remains valid until the subsequent leading clock edge. 16.4.1.4 data polarity the polarity of the transmit and receive data can be controlled with the dtrxp bit of the baudcon regis- ter. the default state of this bit is ? 0 ? which selects high true transmit and receive data. setting the dtrxp bit to ? 1 ? will invert the data resulting in low true transmit and receive data. note: the tsr register is not mapped in data memory, so it is not available to the user.
pic18f/lf1xk50 ds41350e-page 202 preliminary ? 2010 microchip technology inc. 16.4.1.5 synchronous master transmission set-up: 1. initialize the spbrgh, spbrg register pair and the brgh and brg16 bits to achieve the desired baud rate (see section 16.3 ?eusart baud rate generator (brg)? ). 2. enable the synchronous master serial port by setting bits sync, spen and csrc. set the tris bits corresponding to the rx/dt and tx/ck i/o pins. 3. disable receive mode by clearing bits sren and cren. 4. enable transmit mode by setting the txen bit. 5. if 9-bit transmission is desired, set the tx9 bit. 6. if interrupts are desired, set the txie, gie and peie interrupt enable bits. 7. if 9-bit transmission is selected, the ninth bit should be loaded in the tx9d bit. 8. start transmission by loading data to the txreg register. figure 16-10: synchronous transmission figure 16-11: synchronous transmis sion (through txen) bit 0 bit 1 bit 7 word 1 bit 2 bit 0 bit 1 bit 7 rx/dt write to txreg reg txif bit (interrupt flag) txen bit ? 1 ? ? 1 ? word 2 trmt bit write word 1 write word 2 note: sync master mode, spbrg = 0 , continuous transmission of two 8-bit words. pin tx/ck pin tx/ck pin (sckp = 0 ) (sckp = 1 ) rx/dt pin tx/ck pin write to txreg reg txif bit trmt bit bit 0 bit 1 bit 2 bit 6 bit 7 txen bit
? 2010 microchip technology inc. preliminary ds41350e-page 203 pic18f/lf1xk50 table 16-7: registers associated with synchronous master transmission 16.4.1.6 synchronous master reception data is received at the rx/dt pin. the rx/dt pin output driver must be disabled by setting the corresponding tris bits when the eusart is configured for synchronous master receive operation. in synchronous mode, reception is enabled by setting either the single receive enable bit (sren of the rcsta register) or the continuous receive enable bit (cren of the rcsta register). when sren is set and cren is clear, only as many clock cycles are generated as there are data bits in a single character. the sren bit is automatically cleared at the completion of one character. when cren is set, clocks are continuously generated until cren is cleared. if cren is cleared in the middle of a character the ck clock stops immediately and the partial charac- ter is discarded. if sren and cren are both set, then sren is cleared at the completion of the first character and cren takes precedence. to initiate reception, set either sren or cren. data is sampled at the rx/dt pin on the trailing edge of the tx/ck clock pin and is shifted into the receive shift register (rsr). when a complete character is received into the rsr, the rcif bit is set and the character is automatically transferred to the two character receive fifo. the least significant eight bits of the top character in the receive fifo are available in rcreg. the rcif bit remains set as long as there are un-read characters in the receive fifo. 16.4.1.7 slave clock synchronous data transfers use a separate clock line, which is synchronous with the data. a device configured as a slave receives the clock on the tx/ck line. the tx/ck pin output driver must be disabled by setting the associated tris bit when the device is configured for synchronous slave transmit or receive operation. serial data bits change on the leading edge to ensure they are valid at the trailing edge of each clock. one data bit is transferred for each clock cycle. only as many clock cycles should be received as there are data bits. 16.4.1.8 receive overrun error the receive fifo buffer can hold two characters. an overrun error will be generated if a third character, in its entirety, is received before rcreg is read to access the fifo. when this happens the oerr bit of the rcsta register is set. previous data in the fifo will not be overwritten. the two characters in the fifo buffer can be read, however, no additional characters will be received until the error is cleared. the oerr bit can only be cleared by clearing the overrun condition. if the overrun error occurred when the sren bit is set and cren is clear then the error is cleared by reading rcreg. if the overrun occurred when the cren bit is set then the error condition is cleared by either clearing the cren bit of the rcsta register or by clearing the spen bit which resets the eusart. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 285 pir1 ? adif rcif txif sspif ccp1if tmr2if tmr1if 288 pie1 ? adie rcie txie sspie ccp1ie tmr2ie tmr1ie 288 ipr1 ? adip rcip txip sspip ccp1ip tmr2ip tmr1ip 288 rcsta spen rx9 sren cren adden ferr oerr rx9d 287 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 288 txreg eusart transmit register 287 txsta csrc tx9 txen sync sendb brgh trmt tx9d 287 baudcon abdovf rcidl dtrxp cktxp brg16 ? wue abden 287 spbrgh eusart baud rate generator register, high byte 287 spbrg eusart baud rate generator register, low byte 287 legend: ? = unimplemented, read as ? 0 ?. shaded cells are not used for synchronous master transmission.
pic18f/lf1xk50 ds41350e-page 204 preliminary ? 2010 microchip technology inc. 16.4.1.9 receiving 9-bit characters the eusart supports 9-bit character reception. when the rx9 bit of the rcsta register is set the eusart will shift 9-bits into the rsr for each character received. the rx9d bit of the rcsta register is the ninth, and most significant, data bit of the top unread character in the receive fifo. when reading 9-bit data from the receive fifo buffer, the rx9d data bit must be read before reading the 8 least significant bits from the rcreg. 16.4.1.10 synchronous master reception set-up: 1. initialize the spbrgh, spbrg register pair for the appropriate baud rate. set or clear the brgh and brg16 bits, as required, to achieve the desired baud rate. 2. enable the synchronous master serial port by setting bits sync, spen and csrc. disable rx/dt and tx/ck output drivers by setting the corresponding tris bits. 3. ensure bits cren and sren are clear. 4. if using interrupts, set the gie and peie bits of the intcon register and set rcie. 5. if 9-bit reception is desired, set bit rx9. 6. start reception by setting the sren bit or for continuous reception, set the cren bit. 7. interrupt flag bit rcif will be set when reception of a character is complete. an interrupt will be generated if the enable bit rcie was set. 8. read the rcsta register to get the ninth bit (if enabled) and determine if any error occurred during reception. 9. read the 8-bit received data by reading the rcreg register. 10. if an overrun error occurs, clear the error by either clearing the cren bit of the rcsta register or by clearing the spen bit which resets the eusart. figure 16-12: synchronous reception (master mode, sren) cren bit rx/dt write to bit sren sren bit rcif bit (interrupt) read rxreg ? 0 ? bit 0 bit 1 bit 2 bit 3 bit 4 bit 5 bit 6 bit 7 ? 0 ? note: timing diagram demonstrates sync master mode with bit sren = 1 and bit brgh = 0 . tx/ck pin tx/ck pin pin (sckp = 0 ) (sckp = 1 )
? 2010 microchip technology inc. preliminary ds41350e-page 205 pic18f/lf1xk50 table 16-8: registers associated with synchronous master reception 16.4.2 synchronous slave mode the following bits are used to configure the eusart for synchronous slave operation: ? sync = 1 ? csrc = 0 ? sren = 0 (for transmit); sren = 1 (for receive) ? cren = 0 (for transmit); cren = 1 (for receive) ? spen = 1 setting the sync bit of the txsta register configures the device for synchronous operation. clearing the csrc bit of the txsta register configures the device as a slave. clearing the sren and cren bits of the rcsta register ensures that the device is in the transmit mode, otherwise the device will be configured to receive. setting the spen bit of the rcsta register enables the eusart. if the rx/dt or tx/ck pins are shared with an analog peripheral the analog i/o functions must be disabled by clearing the corresponding ansel bits. rx/dt and tx/ck pin output drivers must be disabled by setting the corresponding tris bits. 16.4.2.1 eusart synchronous slave transmit the operation of the synchronous master and slave modes are identical (see section 16.4.1.3 ?synchronous master transmission? ) , except in the case of the sleep mode. if two words are written to the txreg and then the sleep instruction is executed, the following will occur: 1. the first character will immediately transfer to the tsr register and transmit. 2. the second word will remain in txreg register. 3. the txif bit will not be set. 4. after the first character has been shifted out of tsr, the txreg register will transfer the second character to the tsr and the txif bit will now be set. 5. if the peie and txie bits are set, the interrupt will wake the device from sleep and execute the next instruction. if the gie bit is also set, the program will call the interrupt service routine. 16.4.2.2 synchronous slave transmission set-up: 1. set the sync and spen bits and clear the csrc bit. set the tris bits corresponding to the rx/dt and tx/ck i/o pins. 2. clear the cren and sren bits. 3. if using interrupts, ensure that the gie and peie bits of the intcon register are set and set the txie bit. 4. if 9-bit transmission is desired, set the tx9 bit. 5. enable transmission by setting the txen bit. 6. if 9-bit transmission is selected, insert the most significant bit into the tx9d bit. 7. start transmission by writing the least significant 8 bits to the txreg register. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 285 pir1 ? adif rcif txif sspif ccp1if tmr2if tmr1if 288 pie1 ? adie rcie txie sspie ccp1ie tmr2ie tmr1ie 288 ipr1 ? adip rcip txip sspip ccp1ip tmr2ip tmr1ip 288 rcsta spen rx9 sren cren adden ferr oerr rx9d 287 rcreg eusart receive register 287 txsta csrc tx9 txen sync sendb brgh trmt tx9d 287 baudcon abdovf rcidl dtrxp cktxp brg16 ? wue abden 287 spbrgh eusart baud rate genera tor register, high byte 287 spbrg eusart baud rate generator register, low byte 287 legend: ? = unimplemented, read as ? 0 ?. shaded cells are not used for synchronous master reception.
pic18f/lf1xk50 ds41350e-page 206 preliminary ? 2010 microchip technology inc. table 16-9: registers associated with synchronous slave transmission 16.4.2.3 eusart synchronous slave reception the operation of the synchronous master and slave modes is identical ( section 16.4.1.6 ?synchronous master reception? ), with the following exceptions: ? sleep ? cren bit is always set, therefore the receiver is never idle ? sren bit, which is a ?don't care? in slave mode a character may be received while in sleep mode by setting the cren bit prior to entering sleep. once the word is received, the rsr register will transfer the data to the rcreg register. if the rcie enable bit is set, the interrupt generated will wake the device from sleep and execute the next instruction. if the gie bit is also set, the program will branch to the interrupt vector. 16.4.2.4 synchronous slave reception set-up: 1. set the sync and spen bits and clear the csrc bit. set the tris bits corresponding to the rx/dt and tx/ck i/o pins. 2. if using interrupts, ensure that the gie and peie bits of the intcon register are set and set the rcie bit. 3. if 9-bit reception is desired, set the rx9 bit. 4. set the cren bit to enable reception. 5. the rcif bit will be set when reception is complete. an interrupt will be generated if the rcie bit was set. 6. if 9-bit mode is enabled, retrieve the most significant bit from the rx9d bit of the rcsta register. 7. retrieve the 8 least significant bits from the receive fifo by reading the rcreg register. 8. if an overrun error occurs, clear the error by either clearing the cren bit of the rcsta register or by clearing the spen bit which resets the eusart. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 285 pir1 ? adif rcif txif sspif ccp1if tmr2if tmr1if 288 pie1 ? adie rcie txie sspie ccp1ie tmr2ie tmr1ie 288 ipr1 ? adip rcip txip sspip ccp1ip tmr2ip tmr1ip 288 rcsta spen rx9 sren cren adden ferr oerr rx9d 287 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 288 txreg eusart transmit register 287 txsta csrc tx9 txen sync sendb brgh trmt tx9d 287 baudcon abdovf rcidl dtrxp cktxp brg16 ? wue abden 287 spbrgh eusart baud rate generator register, high byte 287 spbrg eusart baud rate generator register, low byte 287 legend: ? = unimplemented, read as ? 0 ?. shaded cells are not used for synchronous master transmission.
? 2010 microchip technology inc. preliminary ds41350e-page 207 pic18f/lf1xk50 table 16-10: registers associated wi th synchronous slave reception name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 285 pir1 ? adif rcif txif sspif ccp1if tmr2if tmr1if 288 pie1 ? adie rcie txie sspie ccp1ie tmr2ie tmr1ie 288 ipr1 ? adip rcip txip sspip ccp1ip tmr2ip tmr1ip 288 rcsta spen rx9 sren cren adden ferr oerr rx9d 287 rcreg eusart receive register 287 txsta csrc tx9 txen sync sendb brgh trmt tx9d 287 baudcon abdovf rcidl dtrxp cktxp brg16 ? wue abden 287 spbrgh eusart baud rate generator register, high byte 287 spbrg eusart baud rate generator register, low byte 287 legend: ? = unimplemented, read as ? 0 ?. shaded cells are not used for synchronous slave reception.
pic18f/lf1xk50 ds41350e-page 208 preliminary ? 2010 microchip technology inc. notes:
? 2010 microchip technology inc. preliminary ds41350e-page 209 pic18f/lf1xk50 17.0 analog-to-digital converter (adc) module the analog-to-digital converter (adc) allows conversion of an analog input signal to a 10-bit binary representation of that signal. this device uses analog inputs, which are multiplexed into a single sample and hold circuit. the output of the sample and hold is connected to the input of the converter. the converter generates a 10-bit binary result via successive approximation and stores the conversion result into the adc result registers (adresl and adresh). the adc voltage reference is software selectable to either v dd , or a voltage applied to the external reference pins. the adc can generate an interrupt upon completion of a conversion. this interrupt can be used to wake-up the device from sleep. figure 17-1 shows the block diagram of the adc. figure 17-1: adc block diagram adc an4 av dd v ref + adon go/done chs<3:0> adresh adresl 10 10 adfm v ss an5 an6 an7 an3 an8 an9 an10 an11 av ss v ref - nvcfg[1:0] = 00 fvr 0000 0001 0010 0011 0100 0101 0111 0110 1000 1001 1010 1011 1100 1101 1110 1111 unused unused 0 = left justify 1 = right justify unused unused unused dac nvcfg[1:0] = 01 fvr pvcfg[1:0] = 00 pvcfg[1:0] = 01 pvcfg[1:0] = 10
pic18f/lf1xk50 ds41350e-page 210 preliminary ? 2010 microchip technology inc. 17.1 adc configuration when configuring and using the adc the following functions must be considered: ? port configuration ? channel selection ? adc voltage reference selection ? adc conversion clock source ? interrupt control ? results formatting 17.1.1 port configuration the ansel, anselh, trisa, trisb and trise reg- isters all configure the a/d port pins. any port pin needed as an analog input should have its correspond- ing ansx bit set to disable the digital input buffer and trisx bit set to disable the digital output driver. if the trisx bit is cleared, the digital output level (v oh or v ol ) will be converted. the a/d operation is independent of the state of the ansx bits and the tris bits. 17.1.2 channel selection the chs bits of the adcon0 register determine which channel is connected to the sample and hold circuit. when changing channels, a delay is required before starting the next conversion. refer to section 17.2 ?adc operation? for more information. 17.1.3 adc v oltage reference the pvcfg and nvcfg bits of the adcon1 register provide independent control of the positive and negative voltage references, respectively. the positive voltage reference can be either v dd , fvr or an external voltage source. the negative voltage reference can be either v ss or an external voltage source. 17.1.4 selecting and configuring acquisition time the adcon2 register allows the user to select an acquisition time that occurs each time the go/done bit is set. acquisition time is set with the acqt<2:0> bits of the adcon2 register. acquisition delays cover a range of 2 to 20 t ad . when the go/done bit is set, the a/d module continues to sample the input for the selected acquisition time, then automatically begins a conver- sion. since the acquisition time is programmed, there is no need to wait for an acquisition time between select- ing a channel and setting the go/done bit. manual acquisition is selected when acqt<2:0> = 000 . when the go/done bit is set, sampling is stopped and a conversion begins. the user is responsible for ensuring the required acquisition time has passed between selecting the desired input channel and setting the go/done bit. this option is also the default reset state of the acqt<2:0> bits and is compatible with devices that do not offer programmable acquisition times. in either case, when the conversion is completed, the go/done bit is cleared, the adif flag is set and the a/d begins sampling the currently selected channel again. when an acquisition time is programmed, there is no indication of when the acquisition time ends and the conversion begins. 17.1.5 conversion clock the source of the conversion clock is software select- able via the adcs bits of the adcon2 register. there are seven possible clock options: ?f osc /2 ?f osc /4 ?f osc /8 ?f osc /16 ?f osc /32 ?f osc /64 ?f rc (dedicated internal oscillator) the time to complete one bit conversion is defined as t ad . one full 10-bit conversion requires 11 t ad periods as shown in figure 17-3 . for correct conversion, the appropriate t ad specification must be met. see a/d conversion requirements in table 27-9 for more information. table 17-1 gives examples of appropriate adc clock selections. note 1: when reading the port register, all pins with their corresponding ansx bit set read as cleared (a low level). however, analog conversion of pins configured as digital inputs (ansx bit cleared and trisx bit set) will be accurately converted. 2: analog levels on any pin with the corre- sponding ansx bit cleared may cause the digital input buffer to consume current out of the device?s specification limits. note: unless using the f rc , any changes in the system clock frequency will change the adc clock frequency, which may adversely affect the adc result.
? 2010 microchip technology inc. preliminary ds41350e-page 211 pic18f/lf1xk50 17.1.6 interrupts the adc module allows for the ability to generate an interrupt upon completion of an analog-to-digital conversion. the adc interrupt flag is the adif bit in the pir1 register. the adc interrupt enable is the adie bit in the pie1 register. the adif bit must be cleared by software. this interrupt can be generated while the device is operating or while in sleep. if the device is in sleep, the interrupt will wake-up the device. upon waking from sleep, the next instruction following the sleep instruction is always executed. if the user is attempting to wake-up from sleep and resume in-line code execution, the global interrupt must be disabled. if the global interrupt is enabled, execution will switch to the interrupt service routine. please see section 17.1.6 ?interrupts? for more information. table 17-1: adc clock period (t ad ) v s . device operating frequencies 17.1.7 result formatting the 10-bit a/d conversion result can be supplied in two formats, left justified or right justified. the adfm bit of the adcon2 register controls the output format. figure 17-2 shows the two output formats. figure 17-2: 10-bit a/d conv ersion result format note: the adif bit is set at the completion of every conversion, regardless of whether or not the adc interrupt is enabled. adc clock period (t ad ) device frequency (f osc ) adc clock source adcs<2:0> 48 mhz 16 mhz 4 mhz 1 mhz f osc /2 000 41.67 ns (2) 125 ns (2) 500 ns (2) 2.0 ? s f osc /4 100 83.33 ns (2) 250 ns (2) 1.0 ? s4.0 ? s f osc /8 001 167 ns (2) 500 ns (2) 2.0 ? s 8.0 ? s (3) f osc /16 101 333 ns (2) 1.0 ? s4.0 ? s 16.0 ? s (3) f osc /32 010 667 ns (2) 2.0 ? s 8.0 ? s (3) 32.0 ? s (3) f osc /64 110 1.33 ? s4.0 ? s 16.0 ? s (3) 64.0 ? s (3) f rc x11 1-4 ? s (1,4) 1-4 ? s (1,4) 1-4 ? s (1,4) 1-4 ? s (1,4) legend: shaded cells are outside of recommended range. note 1: the f rc source has a typical t ad time of 1.7 ? s. 2: these values violate the minimum required t ad time. 3: for faster conversion times, the selection of another clock source is recommended. 4: when the device frequency is greater than 1 mhz, the f rc clock source is only recommended if the conversion will be performed during sleep. adresh adresl (adfm = 0 )msb lsb bit 7 bit 0 bit 7 bit 0 10-bit a/d result unimplemented: read as ? 0 ? (adfm = 1 ) msb lsb bit 7 bit 0 bit 7 bit 0 unimplemented: read as ? 0 ? 10-bit a/d result
pic18f/lf1xk50 ds41350e-page 212 preliminary ? 2010 microchip technology inc. 17.2 adc operation 17.2.1 starting a conversion to enable the adc module, the adon bit of the adcon0 register must be set to a ? 1 ?. setting the go/ done bit of the adcon0 register to a ? 1 ? will, depend- ing on the acqt bits of the adcon2 register, either immediately start the analog-to-digital conversion or start an acquisition delay followed by the analog-to- digital conversion. figure 17-3 shows the operation of the a/d converter after the go bit has been set and the acqt<2:0> bits are cleared. a conversion is started after the following instruction to allow entry into sleep mode before the conversion begins. figure 17-4 shows the operation of the a/d converter after the go bit has been set and the acqt<2:0> bits are set to ? 010 ? which selects a 4 t ad acquisition time before the conversion starts. figure 17-3: a/d conversion t ad cycles (a cqt <2:0> = 000 , t acq = 0 ) figure 17-4: a/d conversion t ad cycles (a cqt <2:0> = 010 , t acq = 4 t ad ) note: the go/done bit should not be set in the same instruction that turns on the adc. refer to section 17.2.9 ?a/d conver- sion procedure? . t ad 1 t ad 2 t ad 3 t ad 4 t ad 5 t ad 6 t ad 7 t ad 8 t ad 11 set go bit holding capacitor is disconnected from analog input (typically 100 ns) t ad 9 t ad 10 t cy - t ad adresh:adresl is loaded, go bit is cleared, adif bit is set, holding capacitor is connected to analog input. conversion starts b0 b9 b6 b5 b4 b3 b2 b1 b8 b7 on the following cycle: 2 t ad discharge 1 2 3 4 5 6 7 8 11 set go bit (holding capacitor is disconnected from analog input) 9 10 conversion starts 1 2 3 4 (holding capacitor continues acquiring input) t acqt cycles t ad cycles automatic acquisition time b0 b9 b6 b5 b4 b3 b2 b1 b8 b7 adresh:adresl is loaded, go bit is cleared, adif bit is set, holding capacitor is connected to analog input. on the following cycle: 2 t ad discharge
? 2010 microchip technology inc. preliminary ds41350e-page 213 pic18f/lf1xk50 17.2.2 completion of a conversion when the conversion is complete, the adc module will: ? clear the go/done bit ? set the adif flag bit ? update the adresh:adresl registers with new conversion result 17.2.3 discharge the discharge phase is used to initialize the value of the capacitor array. the array is discharged after every sample. this feature helps to optimize the unity-gain amplifier, as the circuit always needs to charge the capacitor array, rather than charge/discharge based on previous measure values. 17.2.4 terminating a conversion if a conversion must be terminated before completion, the go/done bit can be cleared by software. the adresh:adresl registers will be updated with the partially complete analog-to-digital conversion sample. unconverted bits will match the last bit converted. 17.2.5 delay between conversions after the a/d conversion is completed or aborted, a 2t ad wait is required before the next acquisition can be started. after this wait, the currently selected channel is reconnected to the charge holding capacitor commencing the next acquisition. 17.2.6 adc operation in power- managed modes the selection of the automatic acquisition time and a/d conversion clock is determined in part by the clock source and frequency while in a power-managed mode. if the a/d is expected to operate while the device is in a power-managed mode, the acqt<2:0> and adcs<2:0> bits in adcon2 should be updated in accordance with the clock source to be used in that mode. after entering the mode, an a/d acquisition or conversion may be started. once started, the device should continue to be clocked by the same clock source until the conversion has been completed. if desired, the device may be placed into the corresponding idle mode during the conversion. if the device clock frequency is less than 1 mhz, the a/d f rc clock source should be selected. 17.2.7 adc operation during sleep the adc module can operate during sleep. this requires the adc clock source to be set to the f rc option. when the f rc clock source is selected, the adc waits one additional instruction before starting the conversion. this allows the sleep instruction to be executed, which can reduce system noise during the conversion. if the adc interrupt is enabled, the device will wake-up from sleep when the conversion completes. if the adc interrupt is disabled, the adc module is turned off after the conversion completes, although the adon bit remains set. when the adc clock source is something other than f rc , a sleep instruction causes the present conver- sion to be aborted and the adc module is turned off, although the adon bit remains set. 17.2.8 special event trigger the ccp1 special event trigger allows periodic adc measurements without software intervention. when this trigger occurs, the go/done bit is set by hardware and the timer1 or timer3 counter resets to zero. using the special event trigger does not assure proper adc timing. it is the user?s responsibility to ensure that the adc timing requirements are met. see section 14.3.4 ?special event trigger? for more information. note: a device reset forces all registers to their reset state. thus, the adc module is turned off and any pending conversion is terminated.
pic18f/lf1xk50 ds41350e-page 214 preliminary ? 2010 microchip technology inc. 17.2.9 a/d conversion procedure this is an example procedure for using the adc to perform an analog-to-digital conversion: 1. configure port: ? disable pin output driver (see tris register) ? configure pin as analog 2. configure the adc module: ? select adc conversion clock ? configure voltage reference ? select adc input channel ? select result format ? select acquisition delay ? turn on adc module 3. configure adc interrupt (optional): ? clear adc interrupt flag ? enable adc interrupt ? enable peripheral interrupt ? enable global interrupt (1) 4. wait the required acquisition time (2) . 5. start conversion by setting the go/done bit. 6. wait for adc conversion to complete by one of the following: ? polling the go/done bit ? waiting for the adc interrupt (interrupts enabled) 7. read adc result 8. clear the adc interrupt flag (required if interrupt is enabled). example 17-1: a/d conversion note 1: the global interrupt can be disabled if the user is attempting to wake-up from sleep and resume in-line code execution. 2: software delay required if acqt bits are set to zero delay. see section 17.3 ?a/d acquisition requirements? . ;this code block configures the adc ;for polling, vdd and vss as reference, frc clock and an4 input. ; ;conversion start & polling for completion ; are included. ; movlw b?10101111? ;right justify, frc, movwf adcon2 ; & 12 tad acq time movlw b?00000000? ;adc ref = vdd,vss movwf adcon1 ; bsf trisc,0 ;set rc0 to input bsf ansel,4 ;set rc0 to analog movlw b?00010001? ;an4, adc on movwf adcon0 ; bsf adcon0,go ;start conversion adcpoll: btfsc adcon0,go ;is conversion done? bra adcpoll ;no, test again ; result is complete - store 2 msbits in ; resulthi and 8 lsbits in resultlo movff adresh,resulthi movff adresl,resultlo
? 2010 microchip technology inc. preliminary ds41350e-page 215 pic18f/lf1xk50 17.2.10 adc register definitions the following registers are used to control the opera- tion of the adc. note: analog pin control is performed by the ansel and anselh registers. for ansel and anselh registers, see register 9-15 and register 9-16 , respectively. register 17-1: adcon0: a/ d control register 0 u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ? ? chs3 chs2 chs1 chs0 go/done adon bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-6 unimplemented: read as ? 0 ? bit 5-2 chs<3:0>: analog channel select bits 0000 = reserved 0001 = reserved 0010 = reserved 0011 = an3 0100 = an4 0101 = an5 0110 = an6 0111 = an7 1000 = an8 1001 = an9 1010 = an10 1011 = an11 1100 = reserved 1101 = reserved 1110 = dac 1111 = fvr bit 1 go/done : a/d conversion status bit 1 = a/d conversion cycle in progress. setting this bit starts an a/d conversion cycle. this bit is automatically cleared by hardware when the a/d conversion has completed. 0 = a/d conversion completed/not in progress bit 0 adon: adc enable bit 1 = adc is enabled 0 = adc is disabled and consumes no operating current note 1: selecting reserved channels will yield unpredictable results as unimplemented input channels are left floating.
pic18f/lf1xk50 ds41350e-page 216 preliminary ? 2010 microchip technology inc. register 17-2: adcon1: a/ d control register 1 u-0 u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 ? ? ? ? pvcfg1 pvcfg0 nvcfg1 nvcfg0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-4 unimplemented: read as ? 0 ? bit 3-2 pvcfg<1:0>: positive voltage reference select bit 00 = positive voltage reference supplied internally by v dd . 01 = positive voltage reference supplied externally through v ref + pin. 10 = positive voltage reference supplied internally through fvr. 11 = reserved. bit 1-0 nvcfg<1:0>: negative voltage reference select bit 00 = negative voltage reference supplied internally by v ss . 01 = negative voltage reference supplied externally through v ref - pin. 10 = reserved. 11 = reserved.
? 2010 microchip technology inc. preliminary ds41350e-page 217 pic18f/lf1xk50 register 17-3: adcon2: a/ d control register 2 r/w-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 adfm ? acqt2 acqt1 acqt0 adcs2 adcs1 adcs0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 adfm: a/d conversion result format select bit 1 = right justified 0 = left justified bit 6 unimplemented: read as ? 0 ? bit 5-3 acqt<2:0>: a/d acquisition time select bits. acquisition time is the duration that the a/d charge holding capacitor remains connected to a/d channel from the instant the go/done bit is set until conversions begins. 000 = 0 (1) 001 = 2 t ad 010 = 4 t ad 011 = 6 t ad 100 = 8 t ad 101 = 12 t ad 110 = 16 t ad 111 = 20 t ad bit 2-0 adcs<2:0>: a/d conversion clock select bits 000 = f osc /2 001 = f osc /8 010 = f osc /32 011 = f rc (1) (clock derived from a dedicated internal oscillator = 600 khz nominal) 100 = f osc /4 101 = f osc /16 110 = f osc /64 111 = f rc (1) (clock derived from a dedicated internal oscillator = 600 khz nominal) note 1: when the a/d clock source is selected as f rc then the start of conversion is delayed by one instruction cycle after the go/done bit is set to allow the sleep instruction to be executed.
pic18f/lf1xk50 ds41350e-page 218 preliminary ? 2010 microchip technology inc. register 17-4: adresh: adc result register high (adresh) adfm = 0 r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x adres9 adres8 adres7 adres6 adres5 adres4 adres3 adres2 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-0 adres<9:2> : adc result register bits upper 8 bits of 10-bit conversion result register 17-5: adresl: adc result register low (adresl) adfm = 0 r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x adres1 adres0 ? ? ? ? ? ? bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-6 adres<1:0> : adc result register bits lower 2 bits of 10-bit conversion result bit 5-0 reserved : do not use. register 17-6: adresh: adc result register high (adresh) adfm = 1 r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x ? ? ? ? ? ? adres9 adres8 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-2 reserved : do not use. bit 1-0 adres<9:8> : adc result register bits upper 2 bits of 10-bit conversion result register 17-7: adresl: adc result register low (adresl) adfm = 1 r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x adres7 adres6 adres5 adres4 adres3 adres2 adres1 adres0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-0 adres<7:0> : adc result register bits lower 8 bits of 10-bit conversion result
? 2010 microchip technology inc. preliminary ds41350e-page 219 pic18f/lf1xk50 17.3 a/d acquisition requirements for the adc to meet its specified accuracy, the charge holding capacitor (c hold ) must be allowed to fully charge to the input channel voltage level. the analog input model is shown in figure 17-5 . the source impedance (r s ) and the internal sampling switch (r ss ) impedance directly affect the time required to charge the capacitor c hold . the sampling switch (r ss ) impedance varies over the device voltage (v dd ), see figure 17-5 . the maximum recommended impedance for analog sources is 10 k ? . as the source impedance is decreased, the acquisition time may be decreased. after the analog input channel is selected (or changed), an a/d acquisition must be done before the conversion can be started. to calculate the minimum acquisition time, equation 17-1 may be used. this equation assumes that 1/2 lsb error is used (1024 steps for the adc). the 1/2 lsb error is the maximum error allowed for the adc to meet its specified resolution. equation 17-1: acquisition time example t acq amplifier settling time hold capacitor charging time temperature coefficient ++ = t amp t c t coff ++ = 5 s t c temperature - 25c ?? 0.05s/c ?? ?? ++ = t c c hold r ic r ss r s ++ ?? ln(1/2047) ? = 13.5pf 1k ? 700 ? 10k ? ++ ?? ? ln(0.0004885) = 1.20 = s t acq 5 s 1.20s 50c- 25c ?? 0.05s/c ?? ?? ++ = 7.45 s = v applied 1e tc ? rc --------- ? ?? ?? ?? v applied 1 1 2047 ----------- - ? ?? ?? = v applied 1 1 2047 ----------- - ? ?? ?? v chold = v applied 1e t c ? rc --------- - ? ?? ?? ?? v chold = ;[1] v chold charged to within 1/2 lsb ;[2] v chold charge response to v applied ;combining [1] and [2] the value for t c can be approximated with the following equations: solving for t c : therefore: temperature 50c and external impedance of 10k ? 3.0 v v dd = assumptions: note 1: the reference voltage (v ref ) has no effect on the equation, since it cancels itself out. 2: the charge holding capacitor (c hold ) is discharged after each conversion. 3: the maximum recommended impedance for analog sources is 10 k ? . this is required to meet the pin leakage specification.
pic18f/lf1xk50 ds41350e-page 220 preliminary ? 2010 microchip technology inc. figure 17-5: analog input model figure 17-6: adc transfer function c pin va rs anx 5 pf v dd v t = 0.6v v t = 0.6v i leakage (1) r ic ? 1k sampling switch ss rss c hold = 13.5 pf v ss /v ref - 2.5v rss (k ? ) 2.0v 1.5v .1 1 10 v dd legend: c pin v t i leakage r ic ss c hold = input capacitance = threshold voltage = leakage current at the pin due to = interconnect resistance = sampling switch = sample/hold capacitance various junctions discharge switch 3.0v 3.5v 100 note 1: see section 27.0 ?electrical specifications? . 3ffh 3feh adc output code 3fdh 3fch 004h 003h 002h 001h 000h full-scale 3fbh 1/2 lsb ideal v ss /v ref - zero-scale transition v dd /v ref + transition 1/2 lsb ideal full-scale range analog input voltage
? 2010 microchip technology inc. preliminary ds41350e-page 221 pic18f/lf1xk50 table 17-2: registers associ ated with a/d operation name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 285 pir1 ? adif rcif txif sspif ccp1if tmr2if tmr1if 288 pie1 ? adie rcie txie sspie ccp1ie tmr2ie tmr1ie 288 ipr1 ? adip rcip txip sspip ccp1ip tmr2ip tmr1ip 288 adresh a/d result register, high byte 287 adresl a/d result register, low byte 287 adcon0 ? ? chs3 chs2 chs1 chs0 go/done adon 287 adcon1 ? ? ? ? pvcfg1 pvcfg0 nvcfg1 nvcfg0 287 adcon2 adfm ? acqt2 acqt1 acqt0 adcs2 adcs1 adcs0 287 ansel ans7 ans6 ans5 ans4 ans3 ? ? ?288 anselh ? ? ? ? ans11 ans10 ans9 ans8 288 trisa ? ? trisa5 trisa4 ? ? ? ? 288 trisb trisb7 trisb6 trisb5 trisb4 ? ? ? ? 288 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 288 legend: ? = unimplemented, read as ? 0 ?. shaded cells are not used for a/d conversion.
pic18f/lf1xk50 ds41350e-page 222 preliminary ? 2010 microchip technology inc. notes:
? 2010 microchip technology inc. preliminary ds41350e-page 223 pic18f/lf1xk50 18.0 comparator module comparators are used to interface analog circuits to a digital circuit by comparing two analog voltages and providing a digital indication of their relative magnitudes. the comparators are very useful mixed signal building blocks because they provide analog functionality independent of the program execution. the analog comparator module includes the following features: ? independent comparator control ? programmable input selection ? comparator output is available internally/externally ? programmable output polarity ? interrupt-on-change ? wake-up from sleep ? programmable speed/power optimization ?pwm shutdown ? programmable and fixed voltage reference 18.1 comparator overview a single comparator is shown in figure 18-1 along with the relationship between the analog input levels and the digital output. when the analog voltage at v in + is less than the analog voltage at v in -, the output of the comparator is a digital low level. when the analog voltage at v in + is greater than the analog voltage at v in -, the output of the comparator is a digital high level. figure 18-1: single comparator ? + v in + v in - output output v in + v in - note: the black areas of the output of the comparator represents the uncertainty due to input offsets and response time.
pic18f/lf1xk50 ds41350e-page 224 preliminary ? 2010 microchip technology inc. figure 18-2: comparator c1 simplified block diagram note 1: when c1on = 0 , the c1 comparator will produce a ? 0 ? output to the xor gate. 2: q1 and q3 are phases of the four-phase system clock (f osc ). 3: q1 is held high during sleep mode. 4: positive going pulse generated on both falling and rising edges of the bit. mux c1 c1pol c1out to pwm logic 0 1 2 3 c1on (1) c1ch<1:0> 2 0 1 c1r mux rd_cm1con0 set c1if to c1v in - c1v in + agnd c12in1- c12in2- c12in3- c1in+ dq en q1 data bus dq en cl q3*rd_cm1con0 nreset + - 0 1 mux v ref c1rsel fvr c1sp c1v ref c1oe c12out 0 1 c1sync from tmr1l[0] (4) dq syncc1out c2oe
? 2010 microchip technology inc. preliminary ds41350e-page 225 pic18f/lf1xk50 figure 18-3: comparator c2 simplified block diagram mux c2 c2pol c2out to pwm logic 0 1 2 3 c2on (1) c2ch<1:0> 2 dq en dq en cl rd_cm2con0 q3*rd_cm2con0 q1 set c2if to nreset c2v in - c2v in + c12out pin agnd c12in1- c12in2- c12in3- data bus note 1: when c2on = 0 , the c2 comparator will produce a ? 0 ? output to the xor gate. 2: q1 and q3 are phases of the four-phase system clock (f osc ). 3: q1 is held high during sleep mode. 4: positive going pulse generated on both falling and rising edges of the bit. 0 1 c2r mux c2in+ 0 1 mux v ref c2rsel fvr c2sp c2v ref 0 1 c2sync c20e dq syncc2out from tmr1l[0] (4)
pic18f/lf1xk50 ds41350e-page 226 preliminary ? 2010 microchip technology inc. 18.2 comparator control each comparator has a separate control and configuration register: cm1con0 for comparator c1 and cm2con0 for comparator c2. in addition, comparator c2 has a second control register, cm2con1, for controlling the interaction with timer1 and simultaneous reading of both comparator outputs. the cm1con0 and cm2con0 registers (see registers 18-1 and 18-2, respectively) contain the control and status bits for the following: ? enable ? input selection ? reference selection ?output selection ? output polarity ? speed selection 18.2.1 comparator enable setting the cxon bit of the cmxcon0 register enables the comparator for operation. clearing the cxon bit disables the comparator resulting in minimum current consumption. 18.2.2 comparator input selection the cxch<1:0> bits of the cmxcon0 register direct one of four analog input pins to the comparator inverting input. 18.2.3 comparator reference selection setting the cxr bit of the cmxcon0 register directs an internal voltage reference or an analog input pin to the non-inverting input of the comparator. see section 21.0 ?v oltage r eferences ? for more information on the internal voltage reference module. 18.2.4 comparator output selection the output of the comparator can be monitored by reading either the cxout bit of the cmxcon0 register or the mcxout bit of the cm2con1 register. in order to make the output available for an external connection, the following conditions must be true: ? cxoe bit of the cmxcon0 register must be set ? corresponding tris bit must be cleared ? cxon bit of the cmxcon0 register must be set both comparators share the same output pin (c12out). priority is determined by the states of the c1oe and c2oe bits. table 18-1: comparator output priority 18.2.5 comparator output polarity inverting the output of the comparator is functionally equivalent to swapping the comparator inputs. the polarity of the comparator output can be inverted by setting the cxpol bit of the cmxcon0 register. clearing the cxpol bit results in a non-inverted output. table 18-2 shows the output state versus input conditions, including polarity control. 18.2.6 comparator speed selection the trade-off between speed or power can be opti- mized during program execution with the cxsp control bit. the default state for this bit is ? 1 ? which selects the normal speed mode. device power consumption can be optimized at the cost of slower comparator propaga- tion delay by clearing the cxsp bit to ? 0 ?. 18.3 comparator response time the comparator output is indeterminate for a period of time after the change of an input source or the selection of a new reference voltage. this period is referred to as the response time. the response time of the comparator differs from the settling time of the voltage reference. therefore, both of these times must be considered when determining the total response time to a comparator input change. see the comparator and voltage reference specifications in section 27.0 ?electrical specifications? for more details. note: to use cxin+ and c12inx- pins as analog inputs, the appropriate bits must be set in the ansel register and the corresponding tris bits must also be set to disable the output drivers. c10e c2oe c12out 00 i/o 01 c2out 10 c1out 11 c2out note 1: the cxoe bit overrides the port data latch. setting the cxon has no impact on the port override. 2: the internal output of the comparator is latched with each instruction cycle. unless otherwise specified, external outputs are not latched. table 18-2: comparator output state vs. input conditions input condition cxpol cxout cxv in - > cxv in + 00 cxv in - < cxv in + 01 cxv in - > cxv in + 11 cxv in - < cxv in + 10
? 2010 microchip technology inc. preliminary ds41350e-page 227 pic18f/lf1xk50 18.4 comparator interrupt operation the comparator interrupt flag can be set whenever there is a change in the output value of the comparator. changes are recognized by means of a mismatch circuit which consists of two latches and an exclusive- or gate (see figure 18-2 and figure 18-3 ). one latch is updated with the comparator output level when the cmxcon0 register is read. this latch retains the value until the next read of the cmxcon0 register or the occurrence of a reset. the other latch of the mismatch circuit is updated on every q1 system clock. a mismatch condition will occur when a comparator output change is clocked through the second latch on the q1 clock cycle. at this point the two mismatch latches have opposite output levels which is detected by the exclusive-or gate and fed to the interrupt circuitry. the mismatch condition persists until either the cmxcon0 register is read or the comparator output returns to the previous state. the comparator interrupt is set by the mismatch edge and not the mismatch level. this means that the inter- rupt flag can be reset without the additional step of reading or writing the cmxcon0 register to clear the mismatch registers. when the mismatch registers are cleared, an interrupt will occur upon the comparator?s return to the previous state, otherwise no interrupt will be generated. software will need to maintain information about the status of the comparator output, as read from the cmxcon0 register, or cm2con1 register, to determine the actual change that has occurred. see figures 18-4 and 18-5 . the cxif bit of the pir2 register is the comparator interrupt flag. this bit must be reset by software by clearing it to ? 0 ?. since it is also possible to write a ? 1 ? to this register, an interrupt can be generated. in mid-range compatibility mode the cxie bit of the pie2 register and the peie and gie bits of the intcon register must all be set to enable comparator interrupts. if any of these bits are cleared, the interrupt is not enabled, although the cxif bit of the pir2 register will still be set if an interrupt condition occurs. 18.4.1 presetting the mismatch latches the comparator mismatch latches can be preset to the desired state before the comparators are enabled. when the comparator is off the cxpol bit controls the cxout level. set the cxpol bit to the desired cxout non-interrupt level while the cxon bit is cleared. then, configure the desired cxpol level in the same instruc- tion that the cxon bit is set. since all register writes are performed as a read-modify-write, the mismatch latches will be cleared during the instruction read phase and the actual configuration of the cxon and cxpol bits will be occur in the final write phase. figure 18-4: comparator interrupt timing w/o cmxcon0 read figure 18-5: comparator interrupt timing with cmxcon0 read note 1: a write operation to the cmxcon0 register will also clear the mismatch condition because all writes include a read operation at the beginning of the write cycle. 2: comparator interrupts will operate correctly regardless of the state of cxoe. note 1: if a change in the cmxcon0 register (cxout) should occur when a read oper- ation is being executed (start of the q2 cycle), then the cxif interrupt flag of the pir2 register may not get set. 2: when either comparator is first enabled, bias circuitry in the comparator module may cause an invalid output from the comparator until the bias circuitry is stable. allow about 1 ? s for bias settling then clear the mismatch condition and interrupt flags before enabling comparator interrupts. q1 q3 cxin+ cxout set cxif (edge) cxif t rt reset by software q1 q3 cxin+ cxout set cxif (edge) cxif t rt reset by software cleared by cmxcon0 read
pic18f/lf1xk50 ds41350e-page 228 preliminary ? 2010 microchip technology inc. 18.5 operation during sleep the comparator, if enabled before entering sleep mode, remains active during sleep. the additional current consumed by the comparator is shown separately in the section 27.0 ?electrical specifications? . if the comparator is not used to wake the device, power consumption can be minimized while in sleep mode by turning off the comparator. each comparator is turned off by clearing the cxon bit of the cmxcon0 register. a change to the comparator output can wake-up the device from sleep. to enable the comparator to wake the device from sleep, the cxie bit of the pie2 register and the peie bit of the intcon register must be set. the instruction following the sleep instruction always executes following a wake from sleep. if the gie bit of the intcon register is also set, the device will then execute the interrupt service routine. 18.6 effects of a reset a device reset forces the cmxcon0 and cm2con1 registers to their reset states. this forces both comparators and the voltage references to their off states.
? 2010 microchip technology inc. preliminary ds41350e-page 229 pic18f/lf1xk50 register 18-1: cm1con0: compa rator 1 control register 0 r/w-0 r-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 c1on c1out c1oe c1pol c1sp c1r c1ch1 c1ch0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 c1on: comparator c1 enable bit 1 = comparator c1 is enabled 0 = comparator c1 is disabled bit 6 c1out: comparator c1 output bit if c1pol = 1 (inverted polarity): c1out = 0 when c1v in + > c1v in - c1out = 1 when c1v in + < c1v in - if c1pol = 0 (non-inverted polarity): c1out = 1 when c1v in + > c1v in - c1out = 0 when c1v in + < c1v in - bit 5 c1oe: comparator c1 output enable bit if c2oe = 0 (c2 output disable) 0 = c1out is internal only 1 = c1out is present on the c12out pin (1) if c2oe = 1 (c2 output enable) 0 = c1out is internal only 1 = c2out is present on the c12out pin (1) bit 4 c1pol: comparator c1 output polarity select bit 1 = c1out logic is inverted 0 = c1out logic is not inverted bit 3 c1sp: comparator c1 speed/power select bit 1 = c1 operates in normal power, higher speed mode 0 = c1 operates in low-power, low-speed mode bit 2 c1r: comparator c1 reference select bit (non-inverting input) 1 = c1v in + connects to c1v ref output 0 = c1v in + connects to c12in+ pin bit 1-0 c1ch<1:0>: comparator c1 channel select bit 00 = c1v in - connects to agnd 01 = c12in1- pin of c1 connects to c1v in - 10 = c12in2- pin of c1 connects to c1v in - 11 = c12in3- pin of c1 connects to c1v in - note 1: comparator output requires the following three conditions: c1oe = 1 , c1on = 1 and corresponding port tris bit = 0 .
pic18f/lf1xk50 ds41350e-page 230 preliminary ? 2010 microchip technology inc. register 18-2: cm2con0: compa rator 2 control register 0 r/w-0 r-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 c2on c2out c2oe c2pol c2sp c2r c2ch1 c2ch0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 c2on: comparator c2 enable bit 1 = comparator c2 is enabled 0 = comparator c2 is disabled bit 6 c2out: comparator c2 output bit if c2pol = 1 (inverted polarity): c2out = 0 when c2v in + > c2v in - c2out = 1 when c2v in + < c2v in - if c2pol = 0 (non-inverted polarity): c2out = 1 when c2v in + > c2v in - c2out = 0 when c2v in + < c2v in - bit 5 c2oe: comparator c2 output enable bit 1 = c2out is present on c12out pin (1) 0 = c2out is internal only bit 4 c2pol: comparator c2 output polarity select bit 1 = c2out logic is inverted 0 = c2out logic is not inverted bit 3 c2sp: comparator c2 speed/power select bit 1 = c2 operates in normal power, higher speed mode 0 = c2 operates in low-power, low-speed mode bit 2 c2r: comparator c2 reference select bits (non-inverting input) 1 = c2v in + connects to c2v ref 0 = c2v in + connects to c2in+ pin bit 1-0 c2ch<1:0>: comparator c2 channel select bits 00 = c1v in - connects to agnd 01 = c12in1- pin of c2 connects to c2v in - 10 = c12in2- pin of c2 connects to c2v in - 11 = c12in3- pin of c2 connects to c2v in - note 1: comparator output requires the following three conditions: c2oe = 1 , c2on = 1 and corresponding port tris bit = 0 .
? 2010 microchip technology inc. preliminary ds41350e-page 231 pic18f/lf1xk50 18.7 analog input connection considerations a simplified circuit for an analog input is shown in figure 18-6 . since the analog input pins share their connection with a digital input, they have reverse biased esd protection diodes to v dd and v ss . the analog input, therefore, must be between v ss and v dd . if the input voltage deviates from this range by more than 0.6v in either direction, one of the diodes is forward biased and a latch-up may occur. a maximum source impedance of 10 k ? is recommended for the analog sources. also, any external component connected to an analog input pin, such as a capacitor or a zener diode, should have very little leakage current to minimize inaccuracies introduced. figure 18-6: analog input model note 1: when reading a port register, all pins configured as analog inputs will read as a ? 0 ?. pins configured as digital inputs will convert as an analog input, according to the input specification. 2: analog levels on any pin defined as a digital input, may cause the input buffer to consume more current than is specified. v a rs < 10k c pin 5 pf v dd v t ? 0.6v v t ? 0.6v r ic i leakage (1) vss a in legend: c pin = input capacitance i leakage = leakage current at the pin due to various junctions r ic = interconnect resistance r s = source impedance v a = analog voltage v t = threshold voltage note 1: see section 27.0 ?electrical specifications? .
pic18f/lf1xk50 ds41350e-page 232 preliminary ? 2010 microchip technology inc. 18.8 additional comparator features there are four additional comparator features: ? simultaneous read of comparator outputs ? internal reference selection ? hysteresis selection ? output synchronization 18.8.1 simultaneous comparator output read the mc1out and mc2out bits of the cm2con1 register are mirror copies of both comparator outputs. the ability to read both outputs simultaneously from a single register eliminates the timing skew of reading separate registers. 18.8.2 internal reference selection there are two internal voltage references available to the non-inverting input of each comparator. one of these is the fixed voltage reference (fvr) and the other is the variable comparator voltage reference (cv ref ). the cxrsel bit of the cm2con register determines which of these references is routed to the comparator voltage reference output (c x v ref ). fur- ther routing to the comparator is accomplished by the cxr bit of the cmxcon0 register. see section 21.1 ?voltage reference? and figure 18-2 and figure 18-3 for more detail. 18.8.3 comparator hysteresis the comparator cx have selectable hysteresis. the hysteresis can be enable by setting the cxhys bit of the cm2con1 register. see section 27.0 ?electrical specifications? for more details. 18.8.4 synchronizing comparator output to timer 1 the comparator cx output can be synchronized with timer1 by setting the cxsync bit of the cm2con1 register. when enabled, the cx output is latched on the rising edge of the timer1 source clock. if a pres- caler is used with timer1, the comparator output is latched after the prescaling function. to prevent a race condition, the comparator output is latched on the rising edge of the timer1 clock source and timer1 increments on the rising edge of its clock source. see the comparator block diagram ( figure 18-2 and figure 18-3 ) and the timer1 block diagram ( figure 18-2 ) for more information. note 1: obtaining the status of c1out or c2out by reading cm2con1 does not affect the comparator interrupt mismatch registers.
? 2010 microchip technology inc. preliminary ds41350e-page 233 pic18f/lf1xk50 register 18-3: cm2con1: compa rator 2 control register 1 r-0 r-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 mc1out mc2out c1rsel c2rsel c1hys c2hys c1sync c2sync bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 mc1out: mirror copy of c1out bit bit 6 mc2out: mirror copy of c2out bit bit 5 c1rsel: comparator c1 reference select bit 1 = fvr routed to c1v ref input 0 = cv ref routed to c1v ref input bit 4 c2rsel: comparator c2 reference select bit 1 = fvr routed to c2v ref input 0 = cv ref routed to c2v ref input bit 3 c1hys: comparator c1 hysteresis enable bit 1 = comparator c1 hysteresis enabled 0 = comparator c1 hysteresis disabled bit 2 c2hys: comparator c2 hysteresis enable bit 1 = comparator c2 hysteresis enabled 0 = comparator c2 hysteresis disabled bit 1 c1sync: c1 output synchronous mode bit 1 = c1 output is synchronous to rising edge to tmr1 clock 0 = c1 output is asynchronous bit 0 c2sync: c2 output synchronous mode bit 1 = c2 output is synchronous to rising edge to tmr1 clock 0 = c2 output is asynchronous
pic18f/lf1xk50 ds41350e-page 234 preliminary ? 2010 microchip technology inc. table 18-3: registers associated with comparator module name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page cm1con0 c1on c1out c1oe c1pol c1sp c1r c1ch1 c1ch0 288 cm2con0 c2on c2out c2oe c2pol c2sp c2r c2ch1 c2ch0 288 cm2con1 mc1out mc2out c1rsel c2rsel c1hys c2hys c1sync c2sync 288 refcon0 fvr1en fvr1st fvr1s1 fvr1s0 ? ? ? ? 287 refcon1 d1en d1lps dac1oe --- d1pss1 d1pss0 ? d1nss 287 intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 285 pir2 oscfif c1if c2if eeif bclif usbif tmr3if ? 288 pie2 oscfie c1ie c2ie eeie bclie usbie tmr3ie ? 288 ipr2 oscfip c1ip c2ip eeip bclip usbip tmr3ip ? 288 portc rc7 rc6 rc5 rc4 rc3 rc2 rc1 rc0 288 latc latc7 latc6 latc5 latc4 latc3 latc2 latc1 latc0 288 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 288 ansel ans7 ans6 ans5 ans4 ans3 ? ? ?288 legend: ? = unimplemented, read as ? 0 ?. shaded cells are unused by the comparator module.
? 2010 microchip technology inc. preliminary ds41350e-page 235 pic18f/lf1xk50 19.0 power-managed modes pic18f/lf1xk50 devices offer a total of seven operat- ing modes for more efficient power management. these modes provide a variety of options for selective power conservation in applications where resources may be limited (i.e., battery-powered devices). there are three categories of power-managed modes: ? run modes ? idle modes ? sleep mode these categories define which portions of the device are clocked and sometimes, what speed. the run and idle modes may use any of the three available clock sources (primary, secondary or internal oscillator block); the sleep mode does not use a clock source. the power-managed modes include several power- saving features offered on previous pic ? microcontroller devices. one is the clock switching feature which allows the controller to use the timer1 oscillator in place of the primary oscillator. also included is the sleep mode, offered by all pic ? microcontroller devices, where all device clocks are stopped. 19.1 selecting power-managed modes selecting a power-managed mode requires two decisions: ? whether or not the cpu is to be clocked ? the selection of a clock source the idlen bit of the osccon register controls cpu clocking, while the scs<1:0> bits of the osccon register select the clock source. the individual modes, bit settings, clock sources and affected modules are summarized in table 19-1 . 19.1.1 clock sources the scs<1:0> bits allow the selection of one of three clock sources for power-managed modes. they are: ? the primary clock, as defined by the fosc<3:0> configuration bits ? the secondary clock (the timer1 oscillator) ? the internal oscillator block 19.1.2 entering power-managed modes switching from one power-managed mode to another begins by loading the osccon register. the scs<1:0> bits select the clock source and determine which run or idle mode is to be used. changing these bits causes an immediate switch to the new clock source, assuming that it is running. the switch may also be subject to clock transition delays. refer to section 2.8 ?clock switching? for more information. entry to the power-managed idle or sleep modes is triggered by the execution of a sleep instruction. the actual mode that results depends on the status of the idlen bit of the osccon register. depending on the current mode and the mode being switched to, a change to a power-managed mode does not always require setting all of these bits. many transitions may be done by changing the oscillator select bits, or changing the idlen bit, prior to issuing a sleep instruction. if the idlen bit is already configured correctly, it may only be necessary to perform a sleep instruction to switch to the desired mode. table 19-1: power-managed modes mode osccon bits module clocking available clock and oscillator source idlen (1) scs<1:0> cpu peripherals sleep 0 n/a off off none ? all clocks are disabled pri_run n/a 00 clocked clocked primary ? lp, xt, hs, rc, ec and internal oscillator block (2) . this is the normal full power execution mode. sec_run n/a 01 clocked clocked secondary ? timer1 oscillator rc_run n/a 1x clocked clocked internal oscillator block (2) pri_idle 100 off clocked primary ? lp, xt, hs, hspll, rc, ec sec_idle 101 off clocked secondary ? timer1 oscillator rc_idle 11x off clocked internal oscillator block (2) note 1: idlen reflects its value when the sleep instruction is executed. 2: includes hfintosc and hfintosc postscal er, as well as the lfintosc source.
pic18f/lf1xk50 ds41350e-page 236 preliminary ? 2010 microchip technology inc. 19.1.3 multiple functions of the sleep command the power-managed mode that is invoked with the sleep instruction is determined by the setting of the idlen bit of the osccon register at the time the instruction is executed. all clocks stop and minimum power is consumed when sleep is executed with the idlen bit cleared. the system clock continues to sup- ply a clock to the peripherals but is disconnected from the cpu when sleep is executed with the idlen bit set. 19.2 run modes in the run modes, clocks to both the core and peripherals are active. the difference between these modes is the clock source. 19.2.1 pri_run mode the pri_run mode is the normal, full power execution mode of the microcontroller. this is also the default mode upon a device reset, unless two-speed start-up is enabled (see section 2.12 ?two-speed start-up mode? for details). in this mode, the device operated off the oscillator defined by the fosc bits of the configh configuration register. 19.2.2 sec_run mode in sec_run mode, the cpu and peripherals are clocked from the secondary external oscillator. this gives users the option of lower power consumption while still using a high accuracy clock source. sec_run mode is entered by setting the scs<1:0> bits of the osccon register to ? 01 ?. when sec_run mode is active all of the following are true: ? the main clock source is switched to the secondary external oscillator ? primary external oscillator is shut down ? t1run bit of the t1con register is set ? osts bit is cleared. 19.2.3 rc_run mode in rc_run mode, the cpu and peripherals are clocked from the internal oscillator. in this mode, the primary external oscillator is shut down. rc_run mode provides the best power conservation of all the run modes when the lfintosc is the system clock. rc_run mode is entered by setting the scs1 bit. when the clock source is switched from the primary oscillator to the internal oscillator, the primary oscillator is shut down and the osts bit is cleared. the ircf bits may be modified at any time to immediately change the clock speed. note: the secondary external oscillator should already be running prior to entering sec_run mode. if the t1oscen bit is not set when the scs<1:0> bits are set to ? 01 ?, entry to sec_run mode will not occur until t1oscen bit is set and sec- ondary external oscillator is ready.
? 2010 microchip technology inc. preliminary ds41350e-page 237 pic18f/lf1xk50 19.3 sleep mode the power-managed sleep mode in the pic18f/ lf1xk50 devices is identical to the legacy sleep mode offered in all other pic ? microcontroller devices. it is entered by clearing the idlen bit of the osccon register and executing the sleep instruction. this shuts down the selected oscillator ( figure 19-1 ) and all clock source status bits are cleared. entering the sleep mode from either run or idle mode does not require a clock switch. this is because no clocks are needed once the controller has entered sleep. if the wdt is selected, the lfintosc source will continue to operate. if the timer1 oscillator is enabled, it will also continue to run. when a wake event occurs in sleep mode (by interrupt, reset or wdt time-out), the device will not be clocked until the clock source selected by the scs<1:0> bits becomes ready (see figure 19-2 ), or it will be clocked from the internal oscillator block if either the two-speed start-up or the fail-safe clock monitor are enabled (see section 24.0 ?special features of the cpu? ). in either case, the osts bit is set when the primary clock is providing the device clocks. the idlen and scs bits are not affected by the wake-up. 19.4 idle modes the idle modes allow the controller?s cpu to be selectively shut down while the peripherals continue to operate. selecting a particular idle mode allows users to further manage power consumption. if the idlen bit is set to a ? 1 ? when a sleep instruction is executed, the peripherals will be clocked from the clock source selected by the scs<1:0> bits; however, the cpu will not be clocked. the clock source status bits are not affected. setting idlen and executing a sleep instruc- tion provides a quick method of switching from a given run mode to its corresponding idle mode. if the wdt is selected, the lfintosc source will con- tinue to operate. if the timer1 oscillator is enabled, it will also continue to run. since the cpu is not executing instructions, the only exits from any of the idle modes are by interrupt, wdt time-out, or a reset. when a wake event occurs, cpu execution is delayed by an interval of t csd while it becomes ready to execute code. when the cpu begins executing code, it resumes with the same clock source for the current idle mode. for example, when waking from rc_idle mode, the internal oscillator block will clock the cpu and peripherals (in other words, rc_run mode). the idlen and scs bits are not affected by the wake-up. while in any idle mode or the sleep mode, a wdt time-out will result in a wdt wake-up to the run mode currently specified by the scs<1:0> bits. figure 19-1: transition timi ng for entry to sleep mode figure 19-2: transition timing for wake from sleep (hspll) q4 q3 q2 osc1 peripheral sleep program q1 q1 counter clock cpu clock pc + 2 pc q3 q4 q1 q2 osc1 peripheral program pc pll clock q3 q4 output cpu clock q1 q2 q3 q4 q1 q2 clock counter pc + 6 pc + 4 q1 q2 q3 q4 wake event note1: t ost = 1024 t osc ; t pll = 2 ms (approx). these intervals are not shown to scale. t ost (1) t pll (1) osts bit set pc + 2
pic18f/lf1xk50 ds41350e-page 238 preliminary ? 2010 microchip technology inc. 19.4.1 pri_idle mode this mode is unique among the three low-power idle modes, in that it does not disable the primary device clock. for timing sensitive applications, this allows for the fastest resumption of device operation with its more accurate primary clock source, since the clock source does not have to ?warm-up? or transition from another oscillator. pri_idle mode is entered from pri_run mode by setting the idlen bit and executing a sleep instruc- tion. if the device is in another run mode, set idlen first, then clear the scs bits and execute sleep . although the cpu is disabled, the peripherals continue to be clocked from the primary clock source specified by the fosc<3:0> configuration bits. the osts bit remains set (see figure 19-3 ). when a wake event occurs, the cpu is clocked from the primary clock source. a delay of interval t csd is required between the wake event and when code execution starts. this is required to allow the cpu to become ready to execute instructions. after the wake- up, the osts bit remains set. the idlen and scs bits are not affected by the wake-up (see figure 19-4 ). 19.4.2 sec_idle mode in sec_idle mode, the cpu is disabled but the peripherals continue to be clocked from the timer1 oscillator. this mode is entered from sec_run by set- ting the idlen bit and executing a sleep instruction. if the device is in another run mode, set the idlen bit first, then set the scs<1:0> bits to ? 01 ? and execute sleep . when the clock source is switched to the timer1 oscillator, the primary oscillator is shut down, the osts bit is cleared and the t1run bit is set. when a wake event occurs, the peripherals continue to be clocked from the timer1 oscillator. after an interval of t csd following the wake event, the cpu begins exe- cuting code being clocked by the timer1 oscillator. the idlen and scs bits are not affected by the wake-up; the timer1 oscillator continues to run (see figure 19- 4 ). figure 19-3: transition timing for entry to idle mode figure 19-4: transition timing for wake from idle to run mode note: the timer1 oscillator should already be running prior to entering sec_idle mode. if the t1oscen bit is not set when the sleep instruction is executed, the main system clock will continue to operate in the previously selected mode and the corresponding idle mode will be entered (i.e., pri_idle or rc_idle). q1 peripheral program pc pc + 2 osc1 q3 q4 q1 cpu clock clock counter q2 osc1 peripheral program pc cpu clock q1 q3 q4 clock counter q2 wake event t csd
? 2010 microchip technology inc. preliminary ds41350e-page 239 pic18f/lf1xk50 19.4.3 rc_idle mode in rc_idle mode, the cpu is disabled but the periph- erals continue to be clocked from the internal oscillator block from the hfintosc multiplexer output. this mode allows for controllable power conservation during idle periods. from rc_run, this mode is entered by setting the idlen bit and executing a sleep instruction. if the device is in another run mode, first set idlen, then set the scs1 bit and execute sleep . it is recommended that scs0 also be cleared, although its value is ignored, to maintain software compatibility with future devices. the hfintosc multiplexer may be used to select a higher clock frequency by modifying the ircf bits before executing the sleep instruction. when the clock source is switched to the hfintosc multiplexer, the primary oscillator is shut down and the osts bit is cleared. if the ircf bits are set to any non-zero value, or the intsrc bit is set, the hfintosc output is enabled. the iosf bit becomes set, after the hfintosc output becomes stable, after an interval of t iobst . clocks to the peripherals continue while the hfintosc source stabilizes. if the ircf bits were previously at a non- zero value, or intsrc was set before the sleep instruction was executed and the hfintosc source was already stable, the iosf bit will remain set. if the ircf bits and intsrc are all clear, the hfintosc output will not be enabled, the iosf bit will remain clear and there will be no indication of the current clock source. when a wake event occurs, the peripherals continue to be clocked from the hfintosc multiplexer output. after a delay of t csd following the wake event, the cpu begins executing code being clocked by the hfintosc multiplexer. the idlen and scs bits are not affected by the wake-up. the lfintosc source will continue to run if either the wdt or the fail-safe clock monitor is enabled. 19.5 exiting idle and sleep modes an exit from sleep mode or any of the idle modes is triggered by any one of the following: ? an interrupt ?a reset ? a watchdog time-out this section discusses the triggers that cause exits from power-managed modes. the clocking subsystem actions are discussed in each of the power-managed modes (see section 19.2 ?run modes? , section 19.3 ?sleep mode? and section 19.4 ?idle modes? ). 19.5.1 exit by interrupt any of the available interrupt sources can cause the device to exit from an idle mode or the sleep mode to a run mode. to enable this functionality, an interrupt source must be enabled by setting its enable bit in one of the intcon or pie registers. the peie bit must also be set if the desired interrupt enable bit is in a pie register. the exit sequence is initiated when the corresponding interrupt flag bit is set. the instruction immediately following the sleep instruction is executed on all exits by interrupt from idle or sleep modes. code execution then branches to the interrupt vector if the gie/gieh bit of the intcon register is set, otherwise code execution continues without branching (see section 7.0 ?interrupts? ). a fixed delay of interval t csd following the wake event is required when leaving sleep and idle modes. this delay is required for the cpu to prepare for execution. instruction execution resumes on the first clock cycle following this delay. 19.5.2 exit by wdt time-out a wdt time-out will cause different actions depending on which power-managed mode the device is in when the time-out occurs. if the device is not executing code (all idle modes and sleep mode), the time-out will result in an exit from the power-managed mode (see section 19.2 ?run modes? and section 19.3 ?sleep mode? ). if the device is executing code (all run modes), the time-out will result in a wdt reset (see section 24.2 ?watch- dog timer (wdt)? ). the wdt timer and postscaler are cleared by any one of the following: ? executing a sleep instruction ? executing a clrwdt instruction ? the loss of the currently selected clock source when the fail-safe clock monitor is enabled ? modifying the ircf bits in the osccon register when the internal oscillator block is the device clock source
pic18f/lf1xk50 ds41350e-page 240 preliminary ? 2010 microchip technology inc. 19.5.3 exit by reset exiting sleep and idle modes by reset causes code execution to restart at address 0. see section 23.0 ?reset? for more details. the exit delay time from reset to the start of code execution depends on both the clock sources before and after the wake-up and the type of oscillator. exit delays are summarized in table 19-2 . 19.5.4 exit without an oscillator start-up delay certain exits from power-managed modes do not invoke the ost at all. there are two cases: ? pri_idle mode, where the primary clock source is not stopped and ? the primary clock source is not any of the lp, xt, hs or hspll modes. in these instances, the primary clock source either does not require an oscillator start-up delay since it is already running (pri_idle), or normally does not require an oscillator start-up delay (rc, ec, intosc, and intoscio modes). however, a fixed delay of interval t csd following the wake event is still required when leaving sleep and idle modes to allow the cpu to prepare for execution. instruction execution resumes on the first clock cycle following this delay. table 19-2: exit delay on wake-up by reset from sleep mode or any idle mode (by clock sources) clock source before wake-up clock source after wake-up exit delay clock ready status bit (osccon) primary device clock (pri_idle mode) lp, xt, hs t csd (1) osts hspll ec, rc hfintosc (2) iosf t1osc or lfintosc (1) lp, xt, hs t ost (3) osts hspll t ost + t pll (3) ec, rc t csd (1) hfintosc (1) t iobst (4) iosf hfintosc (2) lp, xt, hs t ost (4) osts hspll t ost + t pll (3) ec, rc t csd (1) hfintosc (1) none iosf none (sleep mode) lp, xt, hs t ost (3) osts hspll t ost + t pll (3) ec, rc t csd (1) hfintosc (1) t iobst (4) iosf note 1: t csd is a required delay when waking from sleep and all idle modes and runs concurrently with any other required delays (see section 19.4 ?idle modes? ). on reset, hfintosc defaults to 1 mhz. 2: includes both the hfintosc 16 mhz source and postscaler derived frequencies. 3: t ost is the oscillator start-up timer. t pll is the pll lock-out timer (parameter f12 ). 4: execution continues during the hfintosc stabilization period, t iobst .
? 2010 microchip technology inc. preliminary ds41350e-page 241 pic18f/lf1xk50 20.0 sr latch the module consists of a single sr latch with multiple set and reset inputs as well as selectable latch output. the sr latch module includes the following features: ? programmable input selection ? sr latch output is available internally/externally ? selectable q and q output ? firmware set and reset 20.1 latch operation the latch is a set-reset latch that does not depend on a clock source. each of the set and reset inputs are active-high. the latch can be set or reset by cxout, int1 pin, or variable clock. additionally the srps and the srpr bits of the srcon0 register may be used to set or reset the sr latch, respectively. the latch is reset-dominant, therefore, if both set and reset inputs are high the latch will go to the reset state. both the srps and srpr bits are self resetting which means that a single write to either of the bits is all that is necessary to complete a latch set or reset operation. 20.2 latch output the srqen and srnqen bits of the srcon0 register control the latch output selection. only one of the sr latch?s outputs may be directly output to an i/o pin at a time. priority is determined by the state of bits srqen and srnqen in registers srcon0. table 20-1: sr latch output control the applicable tris bit of the corresponding port must be cleared to enable the port pin output driver. 20.3 effects of a reset upon any device reset, the sr latch is not initialized. the user?s firmware is responsible to initialize the latch output before enabling it to the output pins. figure 20-1: sr latch simplified block diagram srlen srqen srnqen sr latch output to port i/o 0x x i/o 10 0 i/o 10 1 q 11 0 q 11 1 q srps s r q q note 1: if r = 1 and s = 1 simultaneously, q = 0 , q = 1 2: pulse generator causes a 2 q-state pulse width. 3: output shown for reference only. see i/ o port pin block diagram for more detail. 4: name denotes the source of connection at the comparator output. pulse gen ( 2 ) sr latch (1) srnqen srq pin (3) srqen srnqen srspe srsc2e int1 srscke srclk syncc2out (4) srsc1e syncc1out (4) srpr pulse gen ( 2 ) srrpe srrc2e int1 srrcke srclk syncc2out (4) srrc1e syncc1out (4) srlen srlen
pic18f/lf1xk50 ds41350e-page 242 preliminary ? 2010 microchip technology inc. table 20-2: srclk frequency table srclk divider f osc = 20 mhz f osc = 16 mhz f osc = 8 mhz f osc = 4 mhz f osc = 1 mhz 111 512 25.6 ? s 32 ? s64 ? s 128 ? s 512 ? s 110 256 12.8 ? s 16 ? s32 ? s64 ? s 256 ? s 101 128 6.4 ? s8 ? s16 ? s32 ? s 128 ? s 100 64 3.2 ? s4 ? s8 ? s16 ? s64 ? s 011 32 1.6 ? s2 ? s4 ? s8 ? s32 ? s 010 16 0.8 ? s1 ? s2 ? s4 ? s16 ? s 001 80.4 ? s0.5 ? s1 ? s2 ? s8 ? s 000 40.2 ? s0.25 ? s0.5 ? s1 ? s4 ? s register 20-1: srcon0: sr latch control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 srlen srclk2 srclk1 srclk0 srqen srnqen srps srpr bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented c = clearable only bit -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 srlen: sr latch enable bit (1) 1 = sr latch is enabled 0 = sr latch is disabled bit 6-4 srclk<2:0> (1) : sr latch clock divider bits 000 = 1/4 peripheral cycle clock 001 = 1/8 peripheral cycle clock 010 = 1/16 peripheral cycle clock 011 = 1/32 peripheral cycle clock 100 = 1/64 peripheral cycle clock 101 = 1/128 peripheral cycle clock 110 = 1/256 peripheral cycle clock 111 = 1/512 peripheral cycle clock bit 3 srqen: sr latch q output enable bit if srnqen = 0 1 = q is present on the rc4 pin 0 = q is internal only bit 2 srnqen: sr latch q output enable bit 1 =q is present on the rc4 pin 0 =q is internal only bit 1 srps: pulse set input of the sr latch 1 = pulse input 0 = always reads back ? 0 ? bit 0 srpr: pulse reset input of the sr latch 1 = pulse input 0 = always reads back ? 0 ? note 1: changing the srclk bits while the sr latch is enabled may cause false triggers to the set and reset inputs of the latch.
? 2010 microchip technology inc. preliminary ds41350e-page 243 pic18f/lf1xk50 table 20-3: registers associated with the sr latch register 20-2: srcon1: sr latch control register 1 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 srspe srscke srsc2e srsc1e srrpe srrcke srrc2e srrc1e bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented c = clearable only bit -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 srspe: sr latch peripheral set enable bit 1 = int1 pin status sets sr latch 0 = int1pin status has no effect on sr latch bit 6 srscke: sr latch set clock enable bit 1 = set input of sr latch is pulsed with srclk 0 = set input of sr latch is not pulsed with srclk bit 5 srsc2e: sr latch c2 set enable bit 1 = c2 comparator output sets sr latch 0 = c2 comparator output has no effect on sr latch bit 4 srsc1e: sr latch c1 set enable bit 1 = c1 comparator output sets sr latch 0 = c1 comparator output has no effect on sr latch bit 3 srrpe: sr latch peripheral reset enable bit 1 = int1 pin resets sr latch 0 = int1 pin has no effect on sr latch bit 2 srrcke: sr latch reset clock enable bit 1 = reset input of sr latch is pulsed with srclk 0 = reset input of sr latch is not pulsed with srclk bit 1 srrc2e: sr latch c2 reset enable bit 1 = c2 comparator output resets sr latch 0 = c2 comparator output has no effect on sr latch bit 0 srrc1e: sr latch c1 reset enable bit 1 = c1 comparator output resets sr latch 0 = c1 comparator output has no effect on sr latch name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page srcon0 srlen srclk2 srclk1 srclk0 srqen srnqen srps srpr 288 srcon1 srspe srscke srsc2e srsc1e srrpe srrcke srrc2e srrc1e 288 cm2con1 mc1out mc2out c1rsel c2rsel c1hys c2hys c1sync c2sync 288 intcon3 int2ip int1ip ? int2ie int1ie ? int2if int1if 285 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 288 legend: shaded cells are not used with the comparator voltage reference.
pic18f/lf1xk50 ds41350e-page 244 preliminary ? 2010 microchip technology inc. notes:
? 2010 microchip technology inc. preliminary ds41350e-page 245 pic18f/lf1xk50 21.0 voltage references there are two independent voltage references available: ? programmable voltage reference ? 1.024v fixed voltage reference 21.1 voltage reference the voltage reference module provides an internally generated voltage reference for the comparators and the dac module. the following features are available: ? independent from comparator operation ? single 32-level voltage ranges ? output clamped to v ss ? ratiometric with v dd ? 1.024v fixed reference voltage (fvr) the refcon1 register ( register 21-2 ) controls the voltage reference module shown in figure 21-1 . 21.1.1 independent operation the voltage reference is independent of the comparator configuration. setting the d1en bit of the refcon1 register will enable the voltage reference by allowing current to flow in the v ref voltage divider. when the d1en bit is cleared, current flow in the v ref voltage divider is disabled minimizing the power drain of the voltage reference peripheral. 21.1.2 output voltage selection the v ref voltage reference has 32 voltage level ranges. the 32 levels are set with the dac1r<4:0> bits of the refcon2 register. the v ref output voltage is determined by the following equations: equation 21-1: v ref output voltage 21.1.3 output ratiometric to v dd the comparator voltage reference is v dd derived and therefore, the v ref output changes with fluctuations in v dd . the tested absolute accuracy of the comparator voltage reference can be found in section 27.0 ?electrical specifications? . 21.1.4 voltage reference output the v ref voltage reference can be output to the device cv ref pin by setting the dac1oe bit of the refcon1 register to ? 1 ?. selecting the reference voltage for out- put on the v ref pin automatically overrides the digital output buffer and digital input threshold detector func- tions of that pin. reading the cv ref pin when it has been configured for reference voltage output will always return a ? 0 ?. due to the limited current drive capability, a buffer must be used on the voltage reference output for external connections to cv ref . figure 21-2 shows an example buffering technique. 21.1.5 operation during sleep when the device wakes up from sleep through an interrupt or a watchdog timer time-out, the contents of the recon1 register are not affected. to minimize current consumption in sleep mode, the voltage reference should be disabled. 21.1.6 effects of a reset a device reset affects the following: ? voltage reference is disabled ? fixed voltage reference is disabled ?v ref is removed from the cv ref pin ? the dac1r<4:0> range select bits are cleared v out v source v source ? ?? x dac1r[4:0] 2 5 -------------------------------- v source + ?? ?? ?? = - if d 1e n = 1 if d 1e n = 0 & d 1l ps = 1 & dac 1 r[4:0] = 11111 : v out v source = + if d 1e n = 0 & d 1l ps = 1 & dac 1 r[4:0] = 00000 : v out v source = - + -
pic18f/lf1xk50 ds41350e-page 246 preliminary ? 2010 microchip technology inc. 21.2 fvr reference module the fvr reference is a stable fixed voltage reference, independent of v dd , with a nominal output voltage of 1.024v. this reference can be enabled by setting the fvr1en bit of the refcon0 register to ? 1 ?. the fvr voltage reference can be routed to the comparators or an adc input channel. 21.2.1 fvr stabilization period when the fixed voltage reference module is enabled, it will require some time for the reference and its amplifier circuits to stabilize. the user program must include a small delay routine to allow the module to settle. the fvr1st stable bit of the refcon0 register also indicates that the fvr reference has been operating long enough to be stable. see section 27.0 ?electrical specifications? for the minimum delay requirement. figure 21-1: voltage reference block diagram 16-to-1 mux dac1r<4:0> r v dd v ref + d1pss<1:0> = 00 d1nss = 0 v ref - d1nss = 1 r r r r r r 32 steps v ref fvr1 d1pss<1:0> = 01 d1pss<1:0> = 10 cv ref pin dac1oe fvr1s<1:0> x 1 x 2 x 4 2 fvr + _ fvr1en fvr1st 1.024v fixed reference d1en d1lps r d1en d1lps
? 2010 microchip technology inc. preliminary ds41350e-page 247 pic18f/lf1xk50 figure 21-2: voltage reference ou tput buffer example register 21-1: refcon0: reference control register 0 r/w-0 r-0 r/w-0 r/w-1 u-0 u-0 u-0 u-0 fvr1en fvr1st fvr1s1 fvr1s0 ? ? ? ? bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 fvr1en: fixed voltage reference 1 enable bit 0 = fvr is disabled 1 = fvr is enabled bit 6 fvr1st: fixed voltage reference 1 stable bit 0 = fvr is not stable 1 = fvr is stable bit 5-4 fvr1s<1:0>: fixed voltage reference 1 voltage select bits 00 = reserved, do not use 01 = 1.024v (x1) 10 = 2.048v (x2) 11 = 4.096v (x4) bit 3-0 unimplemented: read as ? 0 ? buffered cv ref output + ? cv ref module voltage reference output impedance r (1) cv ref note 1: r is dependent upon the voltage reference c onfiguration bits, cvr<3:0> and cvrr. pic18f1xk50/ pic18lf1xk50
pic18f/lf1xk50 ds41350e-page 248 preliminary ? 2010 microchip technology inc. register 21-2: refcon1: reference control register 1 r/w-0 r/w-0 r/w-0 u-0 r/w-0 r/w-0 u-0 r/w-0 d1en d1lps dac1oe --- d1pss1 d1pss0 --- d1nss bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 d1en: dac 1 enable bit 0 = dac 1 is disabled 1 = dac 1 is enabled bit 6 d1lps: dac 1 low-power voltage state select bit 0 =v dac = dac1 negative reference source selected 1 =v dac = dac1 positive reference source selected bit 5 dac1oe: dac 1 voltage output enable bit 1 = dac 1 voltage level is also outputed on the rc2/an6/p1d/c12in2-/cv ref /int2 pin 0 = dac 1 voltage level is disconnected from rc2/an6/p1d/c12in2-/cv ref /int2 pin bit 4 unimplemented: read as ? 0 ? bit 3-2 d1pss<1:0>: dac 1 positive source select bits 00 =v dd 01 =v ref + 10 = fvr output 11 = reserved, do not use bit 1 unimplemented: read as ? 0 ? bit 0 d1nss: dac1 negative source select bits 0 =v ss 1 =v ref - register 21-3: refcon2: reference control register 2 u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 --- --- --- dac1r4 dac1r3 dac1r2 dac1r1 dac1r0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-5 unimplemented: read as ? 0 ? bit 4-0 dac1r<4:0>: dac1 voltage output select bits v out = ((v source +) - (v source -))*(dac1r<4:0>/(2^5)) + v source - note 1: the output select bits are always right justified to ensure that any number of bits can be used without affecting the register layout.
? 2010 microchip technology inc. preliminary ds41350e-page 249 pic18f/lf1xk50 table 21-1: registers associated with voltage reference name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page refcon0 fvr1en fvr1st fvr1s1 fvr1s0 ? ? ? ?287 refcon1 d1en d1lps dac1oe --- d1pss1 d1pss0 ?d1nss287 refcon2 ? ? ? dac1r4 dac1r3 dac1r2 dac1r1 dac1r0 287 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 288 legend: shaded cells are not used with the comparator voltage reference.
pic18f/lf1xk50 ds41350e-page 250 preliminary ? 2010 microchip technology inc. notes:
? 2010 microchip technology inc. preliminary ds41350e-page 251 pic18f/lf1xk50 22.0 universal serial bus (usb) this section describes the details of the usb peripheral. because of the very specific nature of the module, knowledge of usb is expected. some high-level usb information is provided in section 22.10 ?overview of usb? only for application design reference. designers are encouraged to refer to the official specification published by the usb imple- menters forum (usb-if) for the latest information. usb specification revision 2.0 is the most current specification at the time of publication of this document. 22.1 overview of the usb peripheral pic18f1xk50/pic18lf1xk50 devices contain a full-speed and low-speed, compatible usb serial inter- face engine (sie) that allows fast communication between any usb host and the pic ? microcontroller. the sie can be interfaced directly to the usb by utilizing the internal transceiver. some special hardware features have been included to improve performance. dual access port memory in the device?s data memory space (usb ram) has been supplied to share direct memory access between the microcontroller core and the sie. buffer descriptors are also provided, allowing users to freely program end- point memory usage within the usb ram space. figure 22-1 presents a general overview of the usb peripheral and its features. figure 22-1: usb peripheral and options 256 byte usb ram usb sie usb control and transceiver p p d+ d- internal pull-ups external 3.3v supply fsen upuen usb clock from the oscillator module optional external pull-ups (1) (low (full pic18f1xk50/pic18lf1xk50 family usb bus fs speed) speed) note 1: the internal pull-up resistors should be disabled (upuen = 0 ) if external pull-up resistors are used. 2: pic18f13k50/pic18f14k50 only. configuration v usb 3.3v ldo regulator (2)
pic18f/lf1xk50 ds41350e-page 252 preliminary ? 2010 microchip technology inc. 22.2 usb status and control the operation of the usb module is configured and managed through three control registers. in addition, a total of 14 registers are used to manage the actual usb transactions. the registers are: ? usb control register (ucon) ? usb configuration register (ucfg) ? usb transfer status register (ustat) ? usb device address register (uaddr) ? frame number registers (ufrmh:ufrml) ? endpoint enable registers 0 through 7 (uepn) 22.2.1 usb control register (ucon) the usb control register ( register 22-1 ) contains bits needed to control the module behavior during transfers. the register contains bits that control the following: ? main usb peripheral enable ? ping-pong buffer pointer reset ? control of the suspend mode ? packet transfer disable in addition, the usb control register contains a status bit, se0 (ucon<5>), which is used to indicate the occurrence of a single-ended zero on the bus. when the usb module is enabled, this bit should be moni- tored to determine whether the differential data lines have come out of a single-ended zero condition. this helps to differentiate the initial power-up state from the usb reset signal. the overall operation of the usb module is controlled by the usben bit (ucon<3>). setting this bit activates the module and resets all of the ppbi bits in the buffer descriptor table to ? 0 ?. this bit also activates the inter- nal pull-up resistors, if they are enabled. thus, this bit can be used as a soft attach/detach to the usb. although all status and control bits are ignored when this bit is clear, the module needs to be fully preconfig- ured prior to setting this bit. this bit cannot be set until the usb module is supplied with an active clock source. if the pll is being used, it should be enabled at least two milliseconds (enough time for the pll to lock) before attempting to set the usben bit. register 22-1: ucon: usb control register u-0 r/w-0 r-x r/c-0 r/w-0 r/w-0 r/w-0 u-0 ? ppbrst se0 pktdis usben (1) resume suspnd ? bit 7 bit 0 legend: c = clearable bit r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 unimplemented: read as ? 0 ? bit 6 ppbrst: ping-pong buffers reset bit 1 = reset all ping-pong buffer pointers to the even buffer descriptor (bd) banks 0 = ping-pong buffer pointers not being reset bit 5 se0: live single-ended zero flag bit 1 = single-ended zero active on the usb bus 0 = no single-ended zero detected bit 4 pktdis: packet transfer disable bit 1 = sie token and packet processing disabled, automatically set when a setup token is received 0 = sie token and packet processing enabled bit 3 usben: usb module enable bit (1) 1 = usb module and supporting circuitry enabled (device attached) 0 = usb module and supporting circuitry disabled (device detached) bit 2 resume: resume signaling enable bit 1 = resume signaling activated 0 = resume signaling disabled bit 1 suspnd: suspend usb bit 1 = usb module and supporting circuitry in power conserve mode, sie clock inactive 0 = usb module and supporting circuitry in normal operation, sie clock clocked at the configured rate bit 0 unimplemented : read as ? 0 ? note 1: this bit cannot be set if the usb module does not have an appropriate clock source.
? 2010 microchip technology inc. preliminary ds41350e-page 253 pic18f/lf1xk50 the ppbrst bit (ucon<6>) controls the reset status when double-buffering mode (ping-pong buffering) is used. when the ppbrst bit is set, all ping-pong buf- fer pointers are set to the even buffers. ppbrst has to be cleared by firmware. this bit is ignored in buffer- ing modes not using ping-pong buffering. the pktdis bit (ucon<4>) is a flag indicating that the sie has disabled packet transmission and reception. this bit is set by the sie when a setup token is received to allow setup processing. this bit cannot be set by the microcontroller, only cleared; clearing it allows the sie to continue transmission and/or reception. any pending events within the buffer descriptor table will still be available, indicated within the ustat register?s fifo buffer. the resume bit (ucon<2>) allows the peripheral to perform a remote wake-up by executing resume signaling. to generate a valid remote wake-up, firmware must set resume for 10 ms and then clear the bit. for more information on ?resume signaling?, see the ?universal serial bus specification revision 2.0? . the suspnd bit (ucon<1>) places the module and supporting circuitry in a low-power mode. the input clock to the sie is also disabled. this bit should be set by the software in response to an idleif interrupt. it should be reset by the microcontroller firmware after an actvif interrupt is observed. when this bit is active, the device remains attached to the bus but the trans- ceiver outputs remain idle. the voltage on the v usb pin may vary depending on the value of this bit. setting this bit before a idleif request will result in unpredictable bus behavior. 22.2.2 usb configuration register (ucfg) prior to communicating over usb, the module?s associated internal and/or external hardware must be configured. most of the configuration is performed with the ucfg register ( register 22-2 ).the ufcg register contains most of the bits that control the system level behavior of the usb module. these include: ? bus speed (full speed versus low speed) ? on-chip pull-up resistor enable ? ping-pong buffer usage the uteye bit, ucfg<7>, enables eye pattern gener- ation, which aids in module testing, debugging and usb certifications. 22.2.2.1 internal transceiver the usb peripheral has a built-in, usb 2.0, full-speed and low-speed capable transceiver, internally con- nected to the sie. this feature is useful for low-cost, single chip applications. enabling the usb module (usben = 1 ) will also enable the internal transceiver. the fsen bit (ucfg<2>) controls the transceiver speed; setting the bit enables full-speed operation. the on-chip usb pull-up resistors are controlled by the upuen bit (ucfg<4>). they can only be selected when the on-chip transceiver is enabled. the internal usb transceiver obtains power from the v usb pin. in order to meet usb signalling level specifications, v usb must be supplied with a voltage source between 3.0v and 3.6v. the best electrical signal quality is obtained when a 3.3v supply is used and locally bypassed with a high quality ceramic capacitor. the capacitor should be placed as close as possible to the v usb and v ss pins found on the same edge of the package (i.e., route ground of the capacitor to v ss pin 20 on 20-lead pdip, soic, ssop and qfn packaged parts). the d+ and d- signal lines can be routed directly to their respective pins on the usb connector or cable (for hard-wired applications). no additional resistors, capacitors, or magnetic components are required as the d+ and d- drivers have controlled slew rate and output impedance intended to match with the characteristic impedance of the usb cable. in order to meet the usb specifications, the traces should be less than 30 cm long. ideally, these traces should be designed to have a characteristic impedance matching that of the usb cable. note: while in suspend mode, a typical bus-powered usb device is limited to 500 ? a of current. this is the complete current which may be drawn by the pic device and its supporting circuitry. care should be taken to assure minimum current draw when the device enters suspend mode. note: the usb speed, transceiver and pull-up should only be configured during the mod- ule setup phase. it is not recommended to switch these settings while the module is enabled.
pic18f/lf1xk50 ds41350e-page 254 preliminary ? 2010 microchip technology inc. register 22-2: ucfg: us b configuration register r/w-0 u-0 u-0 r/w-0 u-0 r/w-0 r/w-0 r/w-0 uteye ? ? upuen (1) ? fsen (1) ppb1 ppb0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 uteye: usb eye pattern test enable bit 1 = eye pattern test enabled 0 = eye pattern test disabled bit 6-5 unimplemented: read as ? 0 ? bit 4 upuen: usb on-chip pull-up enable bit (1) 1 = on-chip pull-up enabled (pull-up on d+ with fsen = 1 or d- with fsen = 0 ) 0 = on-chip pull-up disabled bit 3 unimplemented: read as ? 0 ? bit 2 fsen: full-speed enable bit (1) 1 = full-speed device: controls transceiver edge rates; requires input clock at 48 mhz 0 = low-speed device: controls transceiver edge rates; requires input clock at 6 mhz bit 1-0 ppb<1:0>: ping-pong buffers configuration bits 11 = even/odd ping-pong buffers enabled for endpoints 1 to 15 10 = even/odd ping-pong buffers enabled for all endpoints 01 = even/odd ping-pong buffer enabled for out endpoint 0 00 = even/odd ping-pong buffers disabled note 1: the upuen, and fsen bits should never be changed while the usb module is enabled. these values must be preconfigured prior to enabling the module.
? 2010 microchip technology inc. preliminary ds41350e-page 255 pic18f/lf1xk50 22.2.2.2 internal pull-up resistors the pic18f1xk50/pic18lf1xk50 devices have built-in pull-up resistors designed to meet the require- ments for low-speed and full-speed usb. the upuen bit (ucfg<4>) enables the internal pull-ups. figure 22-1 shows the pull-ups and their control. 22.2.2.3 external pull-up resistors external pull-up may also be used. the v usb pin may be used to pull up d+ or d-. the pull-up resistor must be 1.5 k ? (5%) as required by the usb specifications. figure 22-2 shows an example. figure 22-2: external circuitry 22.2.2.4 ping-pong buffer configuration the usage of ping-pong buffers is configured using the ppb<1:0> bits. refer to section 22.4.4 ?ping-pong buffering? for a complete explanation of the ping-pong buffers. 22.2.2.5 eye pattern test enable an automatic eye pattern test can be generated by the module when the ucfg<7> bit is set. the eye pattern output will be observable based on module settings, meaning that the user is first responsible for configuring the sie clock settings, pull-up resistor and transceiver mode. in addition, the module has to be enabled. once uteye is set, the module emulates a switch from a receive to transmit state and will start transmitting a j-k-j-k bit sequence (k-j-k-j for full speed). the sequence will be repeated indefinitely while the eye pattern test mode is enabled. note that this bit should never be set while the module is connected to an actual usb system. this test mode is intended for board verification to aid with usb certi- fication tests. it is intended to show a system developer the noise integrity of the usb signals which can be affected by board traces, impedance mismatches and proximity to other system components. it does not properly test the transition from a receive to a transmit state. although the eye pattern is not meant to replace the more complex usb certification test, it should aid during first order system debugging. note: the official usb specifications require that usb devices must never source any current onto the +5v v bus line of the usb cable. additionally, usb devices must never source any current on the d+ and d- data lines whenever the +5v v bus line is less than 1.17v. in order to meet this requirement, applications which are not purely bus powered should monitor the v bus line and avoid turning on the usb module and the d+ or d- pull-up resistor until v bus is greater than 1.17v. v bus can be connected to and monitored by any 5v tolerant i/o pin for this purpose. pic ? microcontroller host controller/hub v usb d+ d- note: the above setting shows a typical connection for a full-speed configuration using an on-chip regulator and an external pull-up resistor. 1.5 k ?
pic18f/lf1xk50 ds41350e-page 256 preliminary ? 2010 microchip technology inc. 22.2.3 usb status register (ustat) the usb status register reports the transaction status within the sie. when the sie issues a usb transfer complete interrupt, ustat should be read to determine the status of the transfer. ustat contains the transfer endpoint number, direction and ping-pong buffer pointer value (if used). the ustat register is actually a read window into a four-byte status fifo, maintained by the sie. it allows the microcontroller to process one transfer while the sie processes additional endpoints ( figure 22-3 ). when the sie completes using a buffer for reading or writing data, it updates the ustat register. if another usb transfer is performed before a transaction complete interrupt is serviced, the sie will store the status of the next transfer into the status fifo. clearing the transfer complete flag bit, trnif, causes the sie to advance the fifo. if the next data in the fifo holding register is valid, the sie will reassert the interrupt within 6 t cy of clearing trnif. if no additional data is present, trnif will remain clear; ustat data will no longer be reliable. figure 22-3: ustat fifo note: the data in the usb status register is valid two sie clocks after the trnif inter- rupt flag is asserted. in low-speed operation with the system clock operating at 48 mhz, a delay may be required between receiving the trnif interrupt and processing the data in the ustat register. note: if an endpoint request is received while the ustat fifo is full, the sie will automatically issue a nak back to the host. data bus ustat from sie 4-byte fifo for ustat clearing trnif advances fifo register 22-3: ustat: usb status register u-0 u-0 r-x r-x r-x r-x r-x u-0 ? ? endp2 endp1 endp0 dir ppbi (1) ? bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-6 unimplemented: read as ? 0 ? bit 5-3 endp<2:0>: encoded number of last endpoint activity bits (represents the number of the bdt updated by the last usb transfer) 111 = endpoint 7 110 = endpoint 6 .... 001 = endpoint 1 000 = endpoint 0 bit 2 dir: last bd direction indicator bit 1 = the last transaction was an in token 0 = the last transaction was an out or setup token bit 1 ppbi: ping-pong bd pointer indicator bit (1) 1 = the last transaction was to the odd bd bank 0 = the last transaction was to the even bd bank bit 0 unimplemented: read as ? 0 ? note 1: this bit is only valid for endpoints with available even and odd bd registers.
? 2010 microchip technology inc. preliminary ds41350e-page 257 pic18f/lf1xk50 22.2.4 usb endpoint control each of the 8 possible bidirectional endpoints has its own independent control register, uepn (where ?n? rep- resents the endpoint number). each register has an identical complement of control bits. the prototype is shown in register 22-4 . the ephshk bit (uepn<4>) controls handshaking for the endpoint; setting this bit enables usb handshaking. typically, this bit is always set except when using isochronous endpoints. the epcondis bit (uepn<3>) is used to enable or disable usb control operations (setup) through the endpoint. clearing this bit enables setup transac- tions. note that the corresponding epinen and epouten bits must be set to enable in and out transactions. for endpoint 0, this bit should always be cleared since the usb specifications identify endpoint 0 as the default control endpoint. the epouten bit (uepn<2>) is used to enable or dis- able usb out transactions from the host. setting this bit enables out transactions. similarly, the epinen bit (uepn<1>) enables or disables usb in transactions from the host. the epstall bit (uepn<0>) is used to indicate a stall condition for the endpoint. if a stall is issued on a particular endpoint, the epstall bit for that end- point pair will be set by the sie. this bit remains set until it is cleared through firmware, or until the sie is reset. register 22-4: uepn: usb endpoint n co ntrol register (uep0 through uep7) u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ? ? ? ephshk epcondis epouten epinen epstall (1) bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-5 unimplemented: read as ? 0 ? bit 4 ephshk: endpoint handshake enable bit 1 = endpoint handshake enabled 0 = endpoint handshake disabled (typically used for isochronous endpoints) bit 3 epcondis: bidirectional endpoint control bit if epouten = 1 and epinen = 1 : 1 = disable endpoint n from control transfers; only in and out transfers allowed 0 = enable endpoint n for control (setup) transfers; in and out transfers also allowed bit 2 epouten: endpoint output enable bit 1 = endpoint n output enabled 0 = endpoint n output disabled bit 1 epinen: endpoint input enable bit 1 = endpoint n input enabled 0 = endpoint n input disabled bit 0 epstall: endpoint stall enable bit (1) 1 = endpoint n is stalled 0 = endpoint n is not stalled note 1: valid only if endpoint n is enabled; otherwise, the bit is ignored.
pic18f/lf1xk50 ds41350e-page 258 preliminary ? 2010 microchip technology inc. 22.2.5 usb address register (uaddr) the usb address register contains the unique usb address that the peripheral will decode when active. uaddr is reset to 00h when a usb reset is received, indicated by urstif, or when a reset is received from the microcontroller. the usb address must be written by the microcontroller during the usb setup phase (enumeration) as part of the microchip usb firmware support. 22.2.6 usb frame number registers (ufrmh:ufrml) the frame number registers contain the 11-bit frame number. the low-order byte is contained in ufrml, while the three high-order bits are contained in ufrmh. the register pair is updated with the current frame number whenever a sof token is received. for the microcontroller, these registers are read-only. the frame number registers are primarily used for isochronous transfers. the contents of the ufrmh and ufrml registers are only valid when the 48 mhz sie clock is active (i.e., contents are inaccurate when suspnd (ucon<1>) bit = 1 ). 22.3 usb ram usb data moves between the microcontroller core and the sie through a memory space known as the usb ram. this is a special dual access memory that is mapped into the normal data memory space in bank 2 (200h to 2ffh) for a total of 256 bytes ( figure 22-4 ). bank 2 (200h through 27fh) is used specifically for endpoint buffer control. depending on the type of buff- ering being used, all but 8 bytes of bank 2 may also be available for use as usb buffer space. although usb ram is available to the microcontroller as data memory, the sections that are being accessed by the sie should not be accessed by the microcontroller. a semaphore mechanism is used to determine the access to a particular buffer at any given time. this is discussed in section 22.4.1.1 ?buffer ownership? . figure 22-4: implementation of usb ram in data memory space 200h 2ffh buffer descriptors, usb data or user data sfrs 1ffh 000h f60h fffh banks 2 (usb ram) f5fh f53h f52h 300h banks 3 to 14 user data unused banks 15 usb data or user data 27fh 280h banks 0 to 1
? 2010 microchip technology inc. preliminary ds41350e-page 259 pic18f/lf1xk50 22.4 buffer descriptors and the buffer descriptor table the registers in bank 2 are used specifically for end- point buffer control in a structure known as the buffer descriptor table (bdt). this provides a flexible method for users to construct and control endpoint buffers of various lengths and configuration. the bdt is composed of buffer descriptors (bd) which are used to define and control the actual buffers in the usb ram space. each bd, in turn, consists of four reg- isters, where n represents one of the 32 possible bds (range of 0 to 31): ? bdnstat: bd status register ? bdncnt: bd byte count register ? bdnadrl: bd address low register ? bdnadrh: bd address high register bds always occur as a four-byte block in the sequence, bdnstat:bdncnt:bdnadrl:bdnadrh. the address of bdnstat is always an offset of (4n ? 1) (in hexa- decimal) from 200h, with n being the buffer descriptor number. depending on the buffering configuration used ( section 22.4.4 ?ping-pong buffering? ), there are up to 16, 17 or 32 sets of buffer descriptors. at a minimum, the bdt must be at least 8 bytes long. this is because the usb specification mandates that every device must have endpoint 0 with both input and output for initial setup. depending on the endpoint and buffering configuration, the bdt can be as long as 128 bytes. although they can be thought of as special function registers, the buffer descriptor status and address registers are not hardware mapped, as conventional microcontroller sfrs in bank 15 are. if the endpoint cor- responding to a particular bd is not enabled, its registers are not used. instead of appearing as unimplemented addresses, however, they appear as available ram. only when an endpoint is enabled by setting the uepn<1> bit does the memory at those addresses become functional as bd registers. as with any address in the data memory space, the bd registers have an indeterminate value on any device reset. an example of a bd for a 64-byte buffer, starting at 280h, is shown in figure 22-5 . a particular set of bd registers is only valid if the corresponding endpoint has been enabled using the uepn register. all bd registers are available in usb ram. the bd for each endpoint should be set up prior to enabling the endpoint. 22.4.1 bd status and configuration buffer descriptors not only define the size of an end- point buffer, but also determine its configuration and control. most of the configuration is done with the bd status register, bdnstat. each bd has its own unique and correspondingly numbered bdnstat register. figure 22-5: example of a buffer descriptor unlike other control registers, the bit configuration for the bdnstat register is context sensitive. there are two distinct configurations, depending on whether the microcontroller or the usb module is modifying the bd and buffer at a particular time. only three bit definitions are shared between the two. 22.4.1.1 buffer ownership because the buffers and their bds are shared between the cpu and the usb module, a simple semaphore mechanism is used to distinguish which is allowed to update the bd and associated buffers in memory. this is done by using the uown bit (bdnstat<7>) as a semaphore to distinguish which is allowed to update the bd and associated buffers in memory. uown is the only bit that is shared between the two configurations of bdnstat. when uown is clear, the bd entry is ?owned? by the microcontroller core. when the uown bit is set, the bd entry and the buffer memory are ?owned? by the usb peripheral. the core should not modify the bd or its corresponding data buffer during this time. note that the microcontroller core can still read bdnstat while the sie owns the buffer and vice versa. the buffer descriptors have a different meaning based on the source of the register update. prior to placing ownership with the usb peripheral, the user can con- figure the basic operation of the peripheral through the bdnstat bits. during this time, the byte count and buf- fer location registers can also be set. when uown is set, the user can no longer depend on the values that were written to the bds. from this point, the sie updates the bds as necessary, overwriting the original bd values. the bdnstat register is updated by the sie with the token pid and the transfer count, bdncnt, is updated. 200h usb data buffer buffer bd0stat bd0cnt bd0adrl bd0adrh 201h 202h 203h 280h 2bfh descriptor note: memory regions not to scale. 40h 00h 05h starting size of block (xxh) registers address contents address
pic18f/lf1xk50 ds41350e-page 260 preliminary ? 2010 microchip technology inc. the bdnstat byte of the bdt should always be the last byte updated when preparing to arm an endpoint. the sie will clear the uown bit when a transaction has completed. no hardware mechanism exists to block access when the uown bit is set. thus, unexpected behavior can occur if the microcontroller attempts to modify memory when the sie owns it. similarly, reading such memory may produce inaccurate data until the usb peripheral returns ownership to the microcontroller. 22.4.1.2 bdnstat register (cpu mode) when uown = 0 , the microcontroller core owns the bd. at this point, the other seven bits of the register take on control functions. the data toggle sync enable bit, dtsen (bdnstat<3>), controls data toggle parity checking. setting dtsen enables data toggle synchronization by the sie. when enabled, it checks the data packet?s par- ity against the value of dts (bdnstat<6>). if a packet arrives with an incorrect synchronization, the data will essentially be ignored. it will not be written to the usb ram and the usb transfer complete interrupt flag will not be set. the sie will send an ack token back to the host to acknowledge receipt, however. the effects of the dtsen bit on the sie are summarized in table 22-1 . the buffer stall bit, bstall (bdnstat<2>), provides support for control transfers, usually one-time stalls on endpoint 0. it also provides support for the set_feature/clear_feature commands speci- fied in chapter 9 of the usb specification; typically, continuous stalls to any endpoint other than the default control endpoint. the bstall bit enables buffer stalls. setting bstall causes the sie to return a stall token to the host if a received token would use the bd in that location. the epstall bit in the corresponding uepn control regis- ter is set and a stall interrupt is generated when a stall is issued to the host. the uown bit remains set and the bds are not changed unless a setup token is received. in this case, the stall condition is cleared and the ownership of the bd is returned to the microcontroller core. the bd<9:8> bits (bdnstat<1:0>) store the two most significant digits of the sie byte count; the lower 8 dig- its are stored in the corresponding bdncnt register. see section 22.4.2 ?bd byte count? for more information. table 22-1: effect of dtsen bit on odd/even (data0/data1) packet reception out packet from host bdnstat settings device response after receiving packet dtsen dts handshake uown trnif bdnstat and ustat status data0 10 ack 01 updated data1 10 ack 10 not updated data0 11 ack 10 not updated data1 11 ack 01 updated either 0x ack 01 updated either, with error xx nak 10 not updated legend: x = don?t care
? 2010 microchip technology inc. preliminary ds41350e-page 261 pic18f/lf1xk50 register 22-5: bdnstat: buffer descriptor n status register (bd0stat through bd31stat), cpu mode (data is written to the side) r/w-x r/w-x u-0 u-0 r/w-x r/w-x r/w-x r/w-x uown (1) dts (2) ? (3) ? (3) dtsen bstall bc9 bc8 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 uown: usb own bit (1) 0 = the microcontroller core owns the bd and its corresponding buffer bit 6 dts: data toggle synchronization bit (2) 1 = data 1 packet 0 = data 0 packet bit 5-4 unimplemented: these bits should always be programmed to ? 0 ? (3) . bit 3 dtsen: data toggle synchronization enable bit 1 = data toggle synchronization is enabled; data packets with incorrect sync value will be ignored except for a setup transaction, which is accepted even if the data toggle bits do not match 0 = no data toggle synchronization is performed bit 2 bstall: buffer stall enable bit 1 = buffer stall enabled; stall handshake issued if a token is received that would use the bd in the given location (uown bit remains set, bd value is unchanged) 0 = buffer stall disabled bit 1-0 bc<9:8>: byte count 9 and 8 bits the byte count bits represent the number of bytes that will be transmitted for an in token or received during an out token. together with bc<7:0>, the valid byte counts are 0-1023. note 1: this bit must be initialized by the user to the desired value prior to enabling the usb module. 2: this bit is ignored unless dtsen = 1 . 3: if these bits are set, usb communication may not work. hence, these bits should always be maintained as ? 0 ?.
pic18f/lf1xk50 ds41350e-page 262 preliminary ? 2010 microchip technology inc. 22.4.1.3 bdnstat register (sie mode) when the bd and its buffer are owned by the sie, most of the bits in bdnstat take on a different meaning. the configuration is shown in register 22-6 . once the uown bit is set, any data or control settings previously written there by the user will be overwritten with data from the sie. the bdnstat register is updated by the sie with the token packet identifier (pid) which is stored in bdnstat<5:3>. the transfer count in the correspond- ing bdncnt register is updated. values that overflow the 8-bit register carry over to the two most significant digits of the count, stored in bdnstat<1:0>. 22.4.2 bd byte count the byte count represents the total number of bytes that will be transmitted during an in transfer. after an in transfer, the sie will return the number of bytes sent to the host. for an out transfer, the byte count represents the maximum number of bytes that can be received and stored in usb ram. after an out transfer, the sie will return the actual number of bytes received. if the number of bytes received exceeds the corresponding byte count, the data packet will be rejected and a nak handshake will be generated. when this happens, the byte count will not be updated. the 10-bit byte count is distributed over two registers. the lower 8 bits of the count reside in the bdncnt register. the upper two bits reside in bdnstat<1:0>. this represents a valid byte range of 0 to 1023. 22.4.3 bd address validation the bd address register pair contains the starting ram address location for the corresponding endpoint buffer. no mechanism is available in hardware to validate the bd address. if the value of the bd address does not point to an address in the usb ram, or if it points to an address within another endpoint?s buffer, data is likely to be lost or overwritten. similarly, overlapping a receive buffer (out endpoint) with a bd location in use can yield unexpected results. when developing usb applications, the user may want to consider the inclusion of software-based address validation in their code. register 22-6: bdnstat: buffer descriptor n status register (bd0stat through bd31stat), sie mode (data returned by the side to the mcu) r/w-x u-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x uown ? pid3 pid2 pid1 pid0 bc9 bc8 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 uown: usb own bit 1 = the sie owns the bd and its corresponding buffer bit 6 reserved: not written by the sie bit 5-2 pid<3:0>: packet identifier bits the received token pid value of the last transfer (in, out or setup transactions only). bit 1-0 bc<9:8>: byte count 9 and 8 bits these bits are updated by the sie to reflect the actual number of bytes received on an out transfer and the actual number of bytes transmitted on an in transfer.
? 2010 microchip technology inc. preliminary ds41350e-page 263 pic18f/lf1xk50 22.4.4 ping-pong buffering an endpoint is defined to have a ping-pong buffer when it has two sets of bd entries: one set for an even transfer and one set for an odd transfer. this allows the cpu to process one bd while the sie is processing the other bd. double-buffering bds in this way allows for maximum throughput to/from the usb. the usb module supports four modes of operation: ? no ping-pong support ? ping-pong buffer support for out endpoint 0 only ? ping-pong buffer support for all endpoints ? ping-pong buffer support for all other endpoints except endpoint 0 the ping-pong buffer settings are configured using the ppb<1:0> bits in the ucfg register. the usb module keeps track of the ping-pong pointer individually for each endpoint. all pointers are initially reset to the even bd when the module is enabled. after the completion of a transaction (uown cleared by the sie), the pointer is toggled to the odd bd. after the completion of the next transaction, the pointer is toggled back to the even bd and so on. the even/odd status of the last transaction is stored in the ppbi bit of the ustat register. the user can reset all ping-pong pointers to even using the ppbrst bit. figure 22-6 shows the four different modes of operation and how usb ram is filled with the bds. bds have a fixed relationship to a particular endpoint, depending on the buffering configuration. the mapping of bds to endpoints is detailed in tab l e 2 2- 2 . this relationship also means that gaps may occur in the bdt if endpoints are not enabled contiguously. this theoretically means that the bds for disabled endpoints could be used as buffer space. in practice, users should avoid using such spaces in the bdt unless a method of validating bd addresses is implemented. figure 22-6: buffer descriptor t able mapping for buffering modes ep1 in even ep1 out even ep1 out odd ep1 in odd descriptor descriptor descriptor descriptor ep1 in ep7 in ep1 out ep0 out ppb<1:0> = 00 ep0 in ep1 in no ping-pong ep7 in ep0 in ep0 out even ppb<1:0> = 01 ep0 out odd ep1 out ping-pong buffer ep7 in odd ep0 in even ep0 out even ppb<1:0> = 10 ep0 out odd ep0 in odd ping-pong buffers descriptor descriptor descriptor descriptor descriptor descriptor descriptor descriptor descriptor descriptor descriptor descriptor 200h 2ffh 2ffh 2ffh 200h 200h 23fh 243h available as data ram available as data ram maximum memory used: 64 bytes maximum bds: 16 (bd0 to bd15) maximum memory used: 68 bytes maximum bds: 17 (bd0 to bd16) maximum memory used: 128 bytes maximum bds: 32 (bd0 to bd31) note: memory area not shown to scale. descriptor descriptor descriptor descriptor buffers on ep0 out on all eps ep1 in even ep1 out even ep1 out odd ep1 in odd descriptor descriptor descriptor descriptor ep7 in odd ep0 out ppb<1:0> = 11 ep0 in ping-pong buffers descriptor descriptor descriptor 2ffh 200h maximum memory used: 120 bytes maximum bds: 30 (bd0 to bd29) on all other eps except ep0 available as data ram 277h 27fh
pic18f/lf1xk50 ds41350e-page 264 preliminary ? 2010 microchip technology inc. table 22-2: assignment of buffer descriptors for the different buffering modes table 22-3: summary of usb buffer descriptor table registers endpoint bds assigned to endpoint mode 0 (no ping-pong) mode 1 (ping-pong on ep0 out) mode 2 (ping-pong on all eps) mode 3 (ping-pong on all other eps, except ep0) out in out in out in out in 0 0 1 0 (e), 1 (o) 2 0 (e), 1 (o) 2 (e), 3 (o) 0 1 1 2 3 3 4 4 (e), 5 (o) 6 (e), 7 (o) 2 (e), 3 (o) 4 (e), 5 (o) 2 4 5 5 6 8 (e), 9 (o) 10 (e), 11 (o) 6 (e), 7 (o) 8 (e), 9 (o) 3 6 7 7 8 12 (e), 13 (o) 14 (e), 15 (o) 10 (e), 11 (o) 12 (e), 13 (o) 4 8 9 9 10 16 (e), 17 (o) 18 (e), 19 (o) 14 (e), 15 (o) 16 (e), 17 (o) 5 10 11 11 12 20 (e), 21 (o) 22 (e), 23 (o) 18 (e), 19 (o) 20 (e), 21 (o) 6 12 13 13 14 24 (e), 25 (o) 26 (e), 27 (o) 22 (e), 23 (o) 24 (e), 25 (o) 7 14 15 15 16 28 (e), 29 (o) 30 (e), 31 (o) 26 (e), 27 (o) 28 (e), 29 (o) legend: (e) = even transaction buffer, (o) = odd transaction buffer name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 bdnstat (1) uown dts (4) pid3 (2) pid2 (2) pid1 (2) dtsen (3) pid0 (2) bstall (3) bc9 bc8 bdncnt (1) byte count bdnadrl (1) buffer address low bdnadrh (1) buffer address high note 1: for buffer descriptor registers, n may have a value of 0 to 31. for the sake of brevity, all 32 registers are shown as one generic prototype. all registers have indeterminate reset values ( xxxx xxxx ). 2: bits 5 through 2 of the bdnstat register are used by the sie to return pid<3:0> values once the register is turned over to the sie (uown bit is set). once the registers have been under sie control, the values written for dtsen and bstall are no longer valid. 3: prior to turning the buffer descriptor over to the sie (uown bit is cleared), bits 5 through 2 of the bdnstat register are used to configure the dtsen and bstall settings. 4: this bit is ignored unless dtsen = 1 .
? 2010 microchip technology inc. preliminary ds41350e-page 265 pic18f/lf1xk50 22.5 usb interrupts the usb module can generate multiple interrupt con- ditions. to accommodate all of these interrupt sources, the module is provided with its own interrupt logic structure, similar to that of the microcontroller. usb interrupts are enabled with one set of control registers and trapped with a separate set of flag registers. all sources are funneled into a single usb interrupt request, usbif (pir2<2>), in the microcontroller?s interrupt logic. figure 22-7 shows the interrupt logic for the usb module. there are two layers of interrupt registers in the usb module. the top level consists of overall usb status interrupts; these are enabled and flagged in the uie and uir registers, respectively. the second level consists of usb error conditions, which are enabled and flagged in the ueir and ueie registers. an interrupt condition in any of these triggers a usb error interrupt flag (uerrif) in the top level. interrupts may be used to trap routine events in a usb transaction. figure 22-8 shows some common events within a usb frame and their corresponding interrupts. figure 22-7: usb interrupt logic funnel figure 22-8: example of a usb transaction and interrupt events btsef btsee btoef btoee dfn8ef dfn8ee crc16ef crc16ee crc5ef crc5ee pidef pidee sofif sofie trnif trnie idleif idleie stallif stallie actvif actvie urstif urstie uerrif uerrie usbif second level usb interrupts (usb error conditions) ueir (flag) and ueie (enable) registers top level usb interrupts (usb status interrupts) uir (flag) and uie (enable) registers usb reset sof reset setup data status sof setup token data ack out token empty data ack start-of-frame (sof) in token data ack sofif urstif 1 ms frame differential data from host from host to h o s t from host to host from host from host from host to h o s t transaction control transfer (1) transaction complete note 1: the control transfer shown here is only an example showing events that can occur for every transaction. typical control transfe rs will spread across multiple frames. set trnif set trnif set trnif
pic18f/lf1xk50 ds41350e-page 266 preliminary ? 2010 microchip technology inc. 22.5.1 usb interrupt status register (uir) the usb interrupt status register ( register 22-7 ) con- tains the flag bits for each of the usb status interrupt sources. each of these sources has a corresponding interrupt enable bit in the uie register. all of the usb status flags are ored together to generate the usbif interrupt flag for the microcontroller?s interrupt funnel. once an interrupt bit has been set by the sie, it must be cleared by software by writing a ? 0 ?. the flag bits can also be set in software which can aid in firmware debugging. register 22-7: uir: usb interrupt status register u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r-0 r/w-0 ? sofif stallif idleif (1) trnif (2) actvif (3) uerrif (4) urstif bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 unimplemented: read as ? 0 ? bit 6 sofif: start-of-frame token interrupt bit 1 = a start-of-frame token received by the sie 0 = no start-of-frame token received by the sie bit 5 stallif: a stall handshake interrupt bit 1 = a stall handshake was sent by the sie 0 = a stall handshake has not been sent bit 4 idleif: idle detect interrupt bit (1) 1 = idle condition detected (constant idle state of 3 ms or more) 0 = no idle condition detected bit 3 trnif: transaction complete interrupt bit (2) 1 = processing of pending transaction is complete; read ustat register for endpoint information 0 = processing of pending transaction is not complete or no transaction is pending bit 2 actvif: bus activity detect interrupt bit (3) 1 = activity on the d+/d- lines was detected 0 = no activity detected on the d+/d- lines bit 1 uerrif: usb error condition interrupt bit (4) 1 = an unmasked error condition has occurred 0 = no unmasked error condition has occurred. bit 0 urstif: usb reset interrupt bit 1 = valid usb reset occurred; 00h is loaded into uaddr register 0 = no usb reset has occurred note 1: once an idle state is detected, the user may want to place the usb module in suspend mode. 2: clearing this bit will cause the ustat fifo to advance (valid only for in, out and setup tokens). 3: this bit is typically unmasked only following the detection of a uidle interrupt event. 4: only error conditions enabled through the ueie register will set this bit. this bit is a status bit only and cannot be set or cleared by the user.
? 2010 microchip technology inc. preliminary ds41350e-page 267 pic18f/lf1xk50 22.5.1.1 bus activity detect interrupt bit (actvif) the actvif bit cannot be cleared immediately after the usb module wakes up from suspend or while the usb module is suspended. a few clock cycles are required to synchronize the internal hardware state machine before the actvif bit can be cleared by firmware. clearing the actv if bit before the internal hardware is synchronized may not have an effect on the value of actvif. additionally, if the usb module uses the clock from the 48 mhz pll source, then after clearing the suspnd bit, the usb module may not be immediately operational while waiting for the 48 mhz pll to lock. the application code should clear the actvif flag as shown in example 22-1 . only one actvif interrupt is generated when resum- ing from the usb bus idle condition. if user firmware clears the actvif bit, the bit will not immediately become set again, even when there is continuous bus traffic. bus traffic must cease long enough to generate another idleif condition before another actvif interrupt can be generated. example 22-1: clearing actvif bit (uir<2>) assembly: bcf ucon, suspnd loop: btfss uir, actvif bra done bcf uir, actvif bra loop done: c: uconbits.suspnd = 0 ; while (uirbits.actvif) { uirbits.actvif = 0 ; }
pic18f/lf1xk50 ds41350e-page 268 preliminary ? 2010 microchip technology inc. 22.5.2 usb interrupt enable register (uie) the usb interrupt enable register ( register 22-8 ) contains the enable bits for the usb status interrupt sources. setting any of these bits will enable the respective interrupt source in the uir register. the values in this register only affect the propagation of an interrupt condition to the microcontroller?s inter- rupt logic. the flag bits are still set by their interrupt conditions, allowing them to be polled and serviced without actually generating an interrupt. register 22-8: uie: usb interrupt enable register u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ? sofie stallie idleie trnie actvie uerrie urstie bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 unimplemented: read as ? 0 ? bit 6 sofie: start-of-frame token interrupt enable bit 1 = start-of-frame token interrupt enabled 0 = start-of-frame token interrupt disabled bit 5 stallie: stall handshake interrupt enable bit 1 = stall interrupt enabled 0 = stall interrupt disabled bit 4 idleie: idle detect interrupt enable bit 1 = idle detect interrupt enabled 0 = idle detect interrupt disabled bit 3 trnie: transaction complete interrupt enable bit 1 = transaction interrupt enabled 0 = transaction interrupt disabled bit 2 actvie: bus activity detect interrupt enable bit 1 = bus activity detect interrupt enabled 0 = bus activity detect interrupt disabled bit 1 uerrie: usb error interrupt enable bit 1 = usb error interrupt enabled 0 = usb error interrupt disabled bit 0 urstie: usb reset interrupt enable bit 1 = usb reset interrupt enabled 0 = usb reset interrupt disabled
? 2010 microchip technology inc. preliminary ds41350e-page 269 pic18f/lf1xk50 22.5.3 usb error interrupt status register (ueir) the usb error interrupt status register ( register 22-9 ) contains the flag bits for each of the error sources within the usb peripheral. each of these sources is controlled by a corresponding interrupt enable bit in the ueie register. all of the usb error flags are ored together to generate the usb error interrupt flag (uerrif) at the top level of the interrupt logic. each error bit is set as soon as the error condition is detected. thus, the interrupt will typically not correspond with the end of a token being processed. once an interrupt bit has been set by the sie, it must be cleared by software by writing a ? 0 ?. register 22-9: ueir: usb erro r interrupt status register r/c-0 u-0 u-0 r/c-0 r/c-0 r/c-0 r/c-0 r/c-0 btsef ? ? btoef dfn8ef crc16ef crc5ef pidef bit 7 bit 0 legend: r = readable bit c = clearable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 btsef: bit stuff error flag bit 1 = a bit stuff error has been detected 0 = no bit stuff error bit 6-5 unimplemented: read as ? 0 ? bit 4 btoef: bus turnaround time-out error flag bit 1 = bus turnaround time-out has occurred (more than 16 bit times of idle from previous eop elapsed) 0 = no bus turnaround time-out bit 3 dfn8ef: data field size error flag bit 1 = the data field was not an integral number of bytes 0 = the data field was an integral number of bytes bit 2 crc16ef: crc16 failure flag bit 1 = the crc16 failed 0 = the crc16 passed bit 1 crc5ef: crc5 host error flag bit 1 = the token packet was rejected due to a crc5 error 0 = the token packet was accepted bit 0 pidef: pid check failure flag bit 1 = pid check failed 0 = pid check passed
pic18f/lf1xk50 ds41350e-page 270 preliminary ? 2010 microchip technology inc. 22.5.4 usb error interrupt enable register (ueie) the usb error interrupt enable register ( register 22-10 ) contains the enable bits for each of the usb error interrupt sources. setting any of these bits will enable the respective error interrupt source in the ueir register to propagate into the uerr bit at the top level of the interrupt logic. as with the uie register, the enable bits only affect the propagation of an interrupt condition to the micro- controller?s interrupt logic. the flag bits are still set by their interrupt conditions, allowing them to be polled and serviced without actually generating an interrupt. register 22-10: ueie: usb error interrupt enable register r/w-0 u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 btsee ? ? btoee dfn8ee crc16ee crc5ee pidee bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 btsee: bit stuff error interrupt enable bit 1 = bit stuff error interrupt enabled 0 = bit stuff error interrupt disabled bit 6-5 unimplemented: read as ? 0 ? bit 4 btoee: bus turnaround time-out error interrupt enable bit 1 = bus turnaround time-out error interrupt enabled 0 = bus turnaround time-out error interrupt disabled bit 3 dfn8ee: data field size error interrupt enable bit 1 = data field size error interrupt enabled 0 = data field size error interrupt disabled bit 2 crc16ee: crc16 failure interrupt enable bit 1 = crc16 failure interrupt enabled 0 = crc16 failure interrupt disabled bit 1 crc5ee: crc5 host error interrupt enable bit 1 = crc5 host error interrupt enabled 0 = crc5 host error interrupt disabled bit 0 pidee: pid check failure interrupt enable bit 1 = pid check failure interrupt enabled 0 = pid check failure interrupt disabled
? 2010 microchip technology inc. preliminary ds41350e-page 271 pic18f/lf1xk50 22.6 usb power modes many usb applications will likely have several different sets of power requirements and configuration. the most common power modes encountered are bus power only, self-power only and dual power with self-power dominance. the most common cases are presented here. also provided is a means of estimating the current consumption of the usb transceiver. 22.6.1 bus power only in bus power only mode, all power for the application is drawn from the usb ( figure 22-9 ). this is effectively the simplest power method for the device. in order to meet the inrush current requirements of the usb 2.0 specifications, the total effective capacitance appearing across v bus and ground must be no more than 10 f. if not, some kind of inrush liming is required. for more details, see section 7.2.4 of the usb 2.0 specification. according to the usb 2.0 specification, all usb devices must also support a low-power suspend mode. in the usb suspend mode, devices must consume no more than 500 ? a (or 2.5 ma for high powered devices that are remote wake-up capable) from the 5v v bus line of the usb cable. the host signals the usb device to enter the suspend mode by stopping all usb traffic to that device for more than 3 ms. this condition will cause the idleif bit in the uir register to become set. during the usb suspend mode, the d+ or d- pull-up resistor must remain active, which will consume some of the allowed suspend current: 500 ? a/2.5 ma budget. figure 22-9: bus power only 22.6.2 self-power only in self-power only mode, the usb application provides its own power, with very little power being pulled from the usb. figure 22-10 shows an example. in order to meet compliance specifications, the usb module (and the d+ or d- pull-up resistor) should not be enabled until the host actively drives v bus high. the application should never source any current onto the 5v v bus pin of the usb cable. figure 22-10: self-power only v dd v usb v ss v bus v dd v usb v ss v self
pic18f/lf1xk50 ds41350e-page 272 preliminary ? 2010 microchip technology inc. 22.6.3 dual power with self-power dominance some applications may require a dual power option. this allows the application to use internal power pri- marily, but switch to power from the usb when no inter- nal power is available. figure 22-11 shows a simple dual power with self-power dominance mode exam- ple, which automatically switches between self-power only and usb bus power only modes. dual power devices must also meet all of the special requirements for inrush current and suspend mode current and must not enable the usb module until v bus is driven high. see section 22.6.1 ?bus power only? and section 22.6.2 ?self-power only? for descriptions of those requirements. additionally, dual power devices must never source current onto the 5v v bus pin of the usb cable. figure 22-11: dual power example 22.6.4 usb transceiver current consumption the usb transceiver consumes a variable amount of current depending on the characteristic impedance of the usb cable, the length of the cable, the v usb supply voltage and the actual data patterns moving across the usb cable. longer cables have larger capacitances and consume more total energy when switching output states. data patterns that consist of ?in? traffic consume far more current than ?out? traffic. in traffic requires the pic ? device to drive the usb cable, whereas out traffic requires that the host drive the usb cable. the data that is sent across the usb cable is nrzi encoded. in the nrzi encoding scheme, ? 0 ? bits cause a toggling of the output state of the transceiver (either from a ?j? state to a ?k? state, or vise versa). with the exception of the effects of bit-stuffing, nrzi encoded ? 1 ? bits do not cause the output state of the transceiver to change. therefore, in traffic consisting of data bits of value, ? 0 ?, cause the most current consumption, as the transceiver must charge/discharge the usb cable in order to change states. more details about nrzi encoding and bit-stuffing can be found in the usb 2.0 specification?s section 7.1, although knowledge of such details is not required to make usb applications using the pic18f1xk50/pic18lf1xk50 of microcontrollers. among other things, the sie handles bit-stuff- ing/unstuffing, nrzi encoding/decoding and crc generation/checking in hardware. the total transceiver current consumption will be application-specific. however, to help estimate how much current actually may be required in full-speed applications, equation 22-1 can be used. example 22-2 shows how this equation can be used for a theoretical application. note: users should keep in mind the limits for devices drawing power from the usb. according to usb specification 2.0, this cannot exceed 100 ma per low-power device or 500 ma per high-power device. v dd v usb v ss v bus v self ~5v ~5v 100 k ?
? 2010 microchip technology inc. preliminary ds41350e-page 273 pic18f/lf1xk50 equation 22-1: estimating usb t ransceiver current consumption example 22-2: calculating usb transceiver current ? i xcvr = + i pullup (60 ma ? v usb ? p zero ? p in ? l cable ) (3.3v ? 5m) legend: v usb : voltage applied to the v usb pin in volts. (should be 3.0v to 3.6v.) p zero : percentage (in decimal) of the in traffic bits sent by the pic ? device that are a value of ? 0 ?. p in : percentage (in decimal) of total bus bandwidth that is used for in traffic. l cable : length (in meters) of the usb cable. the usb 2. 0 specification requires that full-speed applications use cables no longer than 5m. i pullup : current which the nominal, 1.5 k ? pull-up resistor (when enabled) must supply to the usb cable. on the host or hub end of the usb cable, 15 k ? nominal resistors (14.25 k ? to 24.8 k ? ) are present which pull both the d+ and d- lines to ground. during bus id le conditions (such as between packets or during usb suspend mode), this results in up to 218 ? a of quiescent current drawn at 3.3v. i pullup is also dependant on bus traffic conditions and can be as high as 2.2 ma when the usb bandwidth is fully utilized (either in or out traffic) for data that drives the lines to the ?k? state most of the time. for this example, the following assumptions are made about the application: ? 3.3v will be applied to v usb and v dd , with the core voltage regulator enabled. ? this is a full-speed application that uses one interrupt in endpoint that can send one packet of 64 bytes every 1 ms, with no restrictions on the values of the bytes being sent. the application may or may not have addi- tional traffic on out endpoints. ? a regular usb ?b? or ?mini-b? connector will be used on the application circuit board. in this case, p zero = 100% = 1, because there should be no restriction on the value of the data moving through the in endpoint. all 64 kbps of data could potentially be bytes of value, 00h. since ? 0 ? bits cause toggling of the output state of the transceiver, they cause the usb transceiver to consume extra current charging/discharging the cable. in this case, 100% of the data bits sent can be of value ? 0 ?. this should be considered the ?max? value, as normal data will consist of a fair mix of ones and zeros. this application uses 64 kbps for in traffic out of the total bus bandwidth of 1.5 mbps (12 mbps), therefore: since a regular ?b? or ?mini-b? connector is used in this application, the end user may plug in any type of cable up to the maximum allowed 5 m length. therefore, we use the worst-case length: l cable = 5 meters assume i pullup = 2.2 ma. the actual value of i pullup will likely be closer to 218 ? a, but allow for the worst-case. usb bandwidth is shared between all the devices which are plugged into the root port (via hubs). if the application is plugged into a usb 1.1 hub that has other devices plugged into it, your device may see host to device traffic on the bus, even if it is not addressed to your device. since any traffic, regardless of source, can increase the i pullup current above the base 218 ? a, it is safest to allow for the worst-case of 2.2 ma. therefore: the calculated value should be considered an approximation and additional guardband or application-specific prod- uct testing is recommended. the transceiver current is ?in addition to? the rest of the current consumed by the pic18f1xk50/pic18lf1xk50 device that is needed to run the core, drive the other i/o lines, power the various modules, etc. pin = 64 kbps 1.5 mbps = 4.3% = 0.043 i xcvr = + 2.2 ma = 4.8 ma (60 ma ? 3.3v ? 1 ? 0.043 ? 5m) (3.3v ? 5m)
pic18f/lf1xk50 ds41350e-page 274 preliminary ? 2010 microchip technology inc. 22.7 oscillator the usb module has specific clock requirements. for full-speed operation, the clock source must be 48 mhz. even so, the microcontroller core and other peripherals are not required to run at that clock speed. available clocking options are described in detail in section 2.11 ?usb operation? . 22.8 interrupt-on-change for d+/d- pins the pic18f/lf1xk50 has interrupt-on-change func- tionality on both d+ and d- data pins. this feature allows the device to detect voltage level changes when first connected to a usb host/hub. the usb host/hub has 15k pull-down resistors on the d+ and d- pins. when the pic18f/lf1xk50 attaches to the bus the d+ and d- pins can detect voltage changes. external resistors are needed for each pin to maintain a high state on the pins when detached. the usb module must be disable (usben = 0) for the interrupt-on-change to function. enabling the usb module (usben = 1) will automatically disable the interrupt-on-change for d+ and d- pins. refer to section 7.11 ?porta and portb inter- rupt-on-change? for mode detail. 22.9 usb firmware and drivers microchip provides a number of application-specific resources, such as usb firmware and driver support. refer to www.microchip.com for the latest firmware and driver support. table 22-4: registers associat ed with usb module operation (1) name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 details on page: intcon gie/gieh peie/giel tmr0ie int0ie rabie tmr0if int0if rabif 70 ipr2 oscfip c1ip c2ip eeip bcl1ip usbip tmr3ip ?78 pir2 oscfif c1if c2if eeif bcl1if usbif tmr3if ?74 pie2 oscfie c1ie c2ie eeie bcl1ie usbie tmr3ie ?76 ucon ? ppbrst se0 pktdis usben resume suspnd ? 252 ucfg uteye ? ? upuen ? fsen ppb1 ppb0 254 ustat ? endp3 endp2 endp1 endp0 dir ppbi ? 256 uaddr ? addr6 addr5 addr4 addr3 addr2 addr1 addr0 258 ufrml frm7 frm6 frm5 frm4 frm3 frm2 frm1 frm0 252 ufrmh ? ? ? ? ? frm10 frm9 frm8 252 uir ? sofif stallif idleif trnif actvif uerrif urstif 266 uie ? sofie stallie idleie trnie actvie uerrie urstie 268 ueir btsef ? ? btoef dfn8ef crc16ef crc5ef pidef 269 ueie btsee ? ? btoee dfn8ee crc16ee crc5ee pidee 270 uep0 ? ? ? ephshk epcondis epouten epinen epstall 257 uep1 ? ? ? ephshk epcondis epouten epinen epstall 257 uep2 ? ? ? ephshk epcondis epouten epinen epstall 257 uep3 ? ? ? ephshk epcondis epouten epinen epstall 257 uep4 ? ? ? ephshk epcondis epouten epinen epstall 257 uep5 ? ? ? ephshk epcondis epouten epinen epstall 257 uep6 ? ? ? ephshk epcondis epouten epinen epstall 257 uep7 ? ? ? ephshk epcondis epouten epinen epstall 257 legend: ? = unimplemented, read as ? 0 ?. shaded cells are not used by the usb module. note 1: this table includes only those hardware mapped sfrs locate d in bank 15 of the data memory space. the buffer descriptor registers, which are mapped into bank 4 and are not true sfrs, are listed separately in table 22-3 .
? 2010 microchip technology inc. preliminary ds41350e-page 275 pic18f/lf1xk50 22.10 overview of usb this section presents some of the basic usb concepts and useful information necessary to design a usb device. although much information is provided in this section, there is a plethora of information provided within the usb specifications and class specifications. thus, the reader is encouraged to refer to the usb specifications for more information (www.usb.org). if you are very familiar with the details of usb, then this section serves as a basic, high-level refresher of usb. 22.10.1 layered framework usb device functionality is structured into a layered framework graphically shown in figure 22-12 . each level is associated with a functional level within the device. the highest layer, other than the device, is the configuration. a device may have multiple configura- tions. for example, a particular device may have multiple power requirements based on self-power only or bus power only modes. for each configuration, there may be multiple interfaces. each interface could support a particular mode of that configuration. below the interface is the endpoint(s). data is directly moved at this level. there can be as many as 16 bidirectional endpoints. endpoint 0 is always a control endpoint and by default, when the device is on the bus, endpoint 0 must be available to configure the device. 22.10.2 frames information communicated on the bus is grouped into 1 ms time slots, referred to as frames. each frame can contain many transactions to various devices and endpoints. figure 22-8 shows an example of a transaction within a frame. 22.10.3 transfers there are four transfer types defined in the usb specification. ? isochronous: this type provides a transfer method for large amounts of data (up to 1023 bytes) with timely delivery ensured; however, the data integrity is not ensured. this is good for streaming applications where small data loss is not critical, such as audio. ? bulk: this type of transfer method allows for large amounts of data to be transferred with ensured data integrity; however, the delivery timeliness is not ensured. ? interrupt: this type of transfer provides for ensured timely delivery for small blocks of data, plus data integrity is ensured. ? control: this type provides for device setup control. while full-speed devices support all transfer types, low-speed devices are limited to interrupt and control transfers only. 22.10.4 power power is available from the universal serial bus. the usb specification defines the bus power requirements. devices may either be self-powered or bus powered. self-powered devices draw power from an external source, while bus powered devices use power supplied from the bus. figure 22-12: usb layers device configuration interface endpoint interface endpoint endpoint endpoint endpoint to other configurations (if any) to other interfaces (if any)
pic18f/lf1xk50 ds41350e-page 276 preliminary ? 2010 microchip technology inc. the usb specification limits the power taken from the bus. each device is ensured 100 ma at approximately 5v (one unit load). additional power may be requested, up to a maximum of 500 ma. note that power above one unit load is a request and the host or hub is not obligated to provide the extra current. thus, a device capable of consuming more than one unit load must be able to maintain a low-power configuration of a one unit load or less, if necessary. the usb specification also defines a suspend mode. in this situation, current must be limited to 500 ? a, averaged over 1 second. a device must enter a suspend state after 3 ms of inactivity (i.e., no sof tokens for 3 ms). a device entering suspend mode must drop current consumption within 10 ms after suspend. likewise, when signaling a wake-up, the device must signal a wake-up within 10 ms of drawing current above the suspend limit. 22.10.5 enumeration when the device is initially attached to the bus, the host enters an enumeration process in an attempt to identify the device. essentially, the host interrogates the device, gathering information such as power consumption, data rates and sizes, protocol and other descriptive information; descriptors contain this information. a typical enumeration process would be as follows: 1. usb reset: reset the device. thus, the device is not configured and does not have an address (address 0). 2. get device descriptor: the host requests a small portion of the device descriptor. 3. usb reset: reset the device again. 4. set address: the host assigns an address to the device. 5. get device descriptor: the host retrieves the device descriptor, gathering info such as manufacturer, type of device, maximum control packet size. 6. get configuration descriptors. 7. get any other descriptors. 8. set a configuration. the exact enumeration process depends on the host. 22.10.6 descriptors there are eight different standard descriptor types of which five are most important for this device. 22.10.6.1 device descriptor the device descriptor provides general information, such as manufacturer, product number, serial number, the class of the device and the number of configurations. there is only one device descriptor. 22.10.6.2 configuration descriptor the configuration descriptor provides information on the power requirements of the device and how many different interfaces are supported when in this configu- ration. there may be more than one configuration for a device (i.e., low-power and high-power configurations). 22.10.6.3 interface descriptor the interface descriptor details the number of end- points used in this interface, as well as the class of the interface. there may be more than one interface for a configuration. 22.10.6.4 endpoint descriptor the endpoint descriptor identifies the transfer type ( section 22.10.3 ?transfers? ) and direction, as well as some other specifics for the endpoint. there may be many endpoints in a device and endpoints may be shared in different configurations. 22.10.6.5 string descriptor many of the previous descriptors reference one or more string descriptors. string descriptors provide human readable information about the layer ( section 22.10.1 ?layered framework? ) they describe. often these strings show up in the host to help the user identify the device. string descriptors are generally optional to save memory and are encoded in a unicode format. 22.10.7 bus speed each usb device must indicate its bus presence and speed to the host. this is accomplished through a 1.5 k ? resistor which is connected to the bus at the time of the attachment event. depending on the speed of the device, the resistor either pulls up the d+ or d- line to 3.3v. for a low-speed device, the pull-up resistor is connected to the d- line. for a full-speed device, the pull-up resistor is connected to the d+ line. 22.10.8 class specifications and drivers usb specifications include class specifications which operating system vendors optionally support. examples of classes include audio, mass storage, communications and human interface (hid). in most cases, a driver is required at the host side to ?talk? to the usb device. in custom applications, a driver may need to be developed. fortunately, drivers are available for most common host systems for the most common classes of devices. thus, these drivers can be reused.
? 2010 microchip technology inc. preliminary ds41350e-page 277 pic18f/lf1xk50 23.0 reset the pic18f/lf1xk50 devices differentiate between various kinds of reset: a) power-on reset (por) b) mclr reset during normal operation c) mclr reset during power-managed modes d) watchdog timer (wdt) reset (during execution) e) programmable brown-out reset (bor) f) reset instruction g) stack full reset h) stack underflow reset this section discusses resets generated by mclr , por and bor and covers the operation of the various start-up timers. stack reset events are covered in section 3.1.2.4 ?stack full and underflow resets? . wdt resets are covered in section 24.2 ?watchdog timer (wdt)? . a simplified block diagram of the on-chip reset circuit is shown in figure 23-1 . 23.1 rcon register device reset events are tracked through the rcon register ( register 23-1 ). the lower five bits of the reg- ister indicate that a specific reset event has occurred. in most cases, these bits can only be cleared by the event and must be set by the application after the event. the state of these flag bits, taken together, can be read to indicate the type of reset that just occurred. this is described in more detail in section 23.6 ?reset state of registers? . the rcon register also has control bits for setting interrupt priority (ipen) and software control of the bor (sboren). interrupt priority is discussed in section 7.0 ?interrupts? . bor is covered in section 23.4 ?brown-out reset (bor)? . figure 23-1: simplified block diagram of on-chip reset circuit external reset mclr v dd osc1 wdt time-out v dd rise detect ost/pwrt lfintosc por pulse ost (2) 10-bit ripple counter pwrt (2) 11-bit ripple counter enable ost (1) enable pwrt note 1: see table 23-2 for time-out situations. 2: pwrt and ost counters are reset by por and bor. see sections 23.3 and 23.4. brown-out reset boren reset instruction stack pointer stack full/underflow reset sleep ( )_idle 1024 cycles 65.5 ms 32 ? s mclre s r q chip_reset
pic18f/lf1xk50 ds41350e-page 278 preliminary ? 2010 microchip technology inc. register 23-1: rcon: re set control register r/w-0 r/w-1 u-0 r/w-1 r-1 r-1 r/w-0 r/w-0 ipen sboren (1) ?ri to pd por (2) bor bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 ipen: interrupt priority enable bit 1 = enable priority levels on interrupts 0 = disable priority levels on interrupts bit 6 sboren: bor software enable bit (1) if boren<1:0> = 01 : 1 = bor is enabled 0 = bor is disabled if boren<1:0> = 00 , 10 or 11 : bit is disabled and read as ? 0 ?. bit 5 unimplemented: read as ? 0 ? bit 4 ri : reset instruction flag bit 1 = the reset instruction was not executed (set by firmware or power-on reset) 0 = the reset instruction was executed causing a device reset (must be set in firmware after a code-executed reset occurs) bit 3 to : watchdog time-out flag bit 1 = set by power-up, clrwdt instruction or sleep instruction 0 = a wdt time-out occurred bit 2 pd : power-down detection flag bit 1 = set by power-up or by the clrwdt instruction 0 = set by execution of the sleep instruction bit 1 por : power-on reset status bit (2) 1 = no power-on reset occurred 0 = a power-on reset occurred (must be set in software after a power-on reset occurs) bit 0 bor : brown-out reset status bit (3) 1 = a brown-out reset has not occurred (set by firmware only) 0 = a brown-out reset occurred (must be set by firmware after a por or brown-out reset occurs) note 1: if sboren is enabled, its reset state is ? 1 ?; otherwise, it is ? 0 ?. 2: the actual reset value of por is determined by the type of device reset. see the notes following this register and section 23.6 ?reset state of registers? for additional information. 3: see table 23-3 .
? 2010 microchip technology inc. preliminary ds41350e-page 279 pic18f/lf1xk50 23.2 master clear (mclr ) the mclr pin provides a method for triggering an external reset of the device. a reset is generated by holding the pin low. these devices have a noise filter in the mclr reset path which detects and ignores small pulses. the mclr pin is not driven low by any internal resets, including the wdt. in pic18f/lf1xk50 devices, the mclr input can be disabled with the mclre configuration bit. when mclr is disabled, the pin becomes a digital input. see section 9.1 ?porta, trisa and lata registers? for more information. 23.3 power-on reset (por) a power-on reset pulse is generated on-chip whenever v dd rises above a certain threshold. this allows the device to start in the initialized state when v dd is adequate for operation. to take advantage of the por circuitry, tie the mclr pin through a resistor (1 k ? to 10 k ? ) to v dd . this will eliminate external rc components usually needed to create a power-on reset delay. when the device starts normal operation (i.e., exits the reset condition), device operating parameters (volt- age, frequency, temperature, etc.) must be met to ensure operation. if these conditions are not met, the device must be held in reset until the operating conditions are met. por events are captured by the por bit of the rcon register. the state of the bit is set to ? 0 ? whenever a por occurs; it does not change for any other reset event. por is not reset to ? 1 ? by any hardware event. to capture multiple events, the user must manually set the bit to ? 1 ? by software following any por. figure 23-2: external power-on reset circuit (for slow v dd power-up) note 1: external power-on reset circuit is required only if the v dd power-up slope is too slow. the diode d helps discharge the capacitor quickly when v dd powers down. 2: r < 40 k ? is recommended to make sure that the voltage drop across r does not violate the device?s electric al specification. 3: r1 ? 1 k ? will limit any current flowing into mclr from external capacitor c, in the event of mclr /v pp pin breakdown, due to electrostatic discharge (esd) or electrical overstress (eos). c r1 r d v dd mclr v dd pic ? mcu
pic18f/lf1xk50 ds41350e-page 280 preliminary ? 2010 microchip technology inc. 23.4 brown-out reset (bor) pic18f/lf1xk50 devices implement a bor circuit that provides the user with a number of configuration and power-saving options. the bor is controlled by the borv<1:0> and boren<1:0> bits of the config2l configuration register. there are a total of four bor configurations which are summarized in table 23-1 . the bor threshold is set by the borv<1:0> bits. if bor is enabled (any values of boren<1:0>, except ? 00 ?), any drop of v dd below v bor for greater than t bor will reset the device. a reset may or may not occur if v dd falls below v bor for less than t bor . the chip will remain in brown-out reset until v dd rises above v bor . if the power-up timer is enabled, it will be invoked after v dd rises above v bor ; it then will keep the chip in reset for an additional time delay, t pwrt . if v dd drops below v bor while the power-up timer is running, the chip will go back into a brown-out reset and the power-up timer will be initialized. once v dd rises above v bor , the power-up timer will execute the additional time delay. bor and the power-on timer (pwrt) are independently configured. enabling bor reset does not automatically enable the pwrt. 23.4.1 software enabled bor when boren<1:0> = 01 , the bor can be enabled or disabled by the user in software. this is done with the sboren control bit of the rcon register. setting sboren enables the bor to function as previously described. clearing sboren disables the bor entirely. the sboren bit operates only in this mode; otherwise it is read as ? 0 ?. placing the bor under software control gives the user the additional flexibility of tailoring the application to its environment without having to reprogram the device to change bor configuration. it also allows the user to tailor device power consumption in software by eliminating the incremental current that the bor consumes. while the bor current is typically very small, it may have some impact in low-power applications. 23.4.2 detecting bor when bor is enabled, the bor bit always resets to ? 0 ? on any bor or por event. this makes it difficult to determine if a bor event has occurred just by reading the state of bor alone. a more reliable method is to simultaneously check the state of both por and bor . this assumes that the por and bor bits are reset to ? 1 ? by software immediately after any por event. if bor is ? 0 ? while por is ? 1 ?, it can be reliably assumed that a bor event has occurred. 23.4.3 disabling bor in sleep mode when boren<1:0> = 10 , the bor remains under hardware control and operates as previously described. whenever the device enters sleep mode, however, the bor is automatically disabled. when the device returns to any other operating mode, bor is automatically re-enabled. this mode allows for applications to recover from brown-out situations, while actively executing code, when the device requires bor protection the most. at the same time, it saves additional power in sleep mode by eliminating the small incremental bor current. table 23-1: bor configurations note: even when bor is under software con- trol, the bor reset voltage level is still set by the borv<1:0> configuration bits. it cannot be changed by software. bor configuration status of sboren (rcon<6>) bor operation boren1 boren0 00 unavailable bor disabled; must be enabled by reprogramming the configuration bits. 01 available bor enabled by software; operation controlled by sboren. 10 unavailable bor enabled by hardware in run and idle modes, disabled during sleep mode. 11 unavailable bor enabled by hardware; must be disabled by reprogramming the configuration bits.
? 2010 microchip technology inc. preliminary ds41350e-page 281 pic18f/lf1xk50 23.5 device reset timers pic18f/lf1xk50 devices incorporate three separate on-chip timers that help regulate the power-on reset process. their main function is to ensure that the device clock is stable before code is executed. these timers are: ? power-up timer (pwrt) ? oscillator start-up timer (ost) ? pll lock time-out 23.5.1 power-up timer (pwrt) the power-up timer (pwrt) of pic18f/lf1xk50 devices is an 11-bit counter which uses the lfin- tosc source as the clock input. this yields an approximate time interval of 2048 x 32 ? s=65.6ms. while the pwrt is counting, the device is held in reset. the power-up time delay depends on the lfintosc clock and will vary from chip-to-chip due to temperature and process variation. see section 27.0 ?electrical specifications? for details. the pwrt is enabled by clearing the pwrten configuration bit. 23.5.2 oscillator start-up timer (ost) the oscillator start-up timer (ost) provides a 1024 oscillator cycle (from osc1 input) delay after the pwrt delay is over. this ensures that the crystal oscillator or resonator has started and stabilized. the ost time-out is invoked only for xt, lp, hs and hspll modes and only on power-on reset, or on exit from all power-managed modes that stop the external oscillator. 23.5.3 pll lock time-out with the pll enabled in its pll mode, the time-out sequence following a power-on reset is slightly different from other oscillator modes. a separate timer is used to provide a fixed time-out that is sufficient for the pll to lock to the main oscillator frequency. this pll lock time-out (t pll ) is typically 2 ms and follows the oscillator start-up time-out. 23.5.4 time-out sequence on power-up, the time-out sequence is as follows: 1. after the por pulse has cleared, pwrt time-out is invoked (if enabled). 2. then, the ost is activated. the total time-out will vary based on oscillator configuration and the status of the pwrt. figure 23-3 , figure 23-4 , figure 23-5 , figure 23-6 and figure 23-7 all depict time-out sequences on power-up, with the power-up timer enabled and the device operating in hs oscillator mode. figures 23-3 through 23-6 also apply to devices operating in xt or lp modes. for devices in rc mode and with the pwrt disabled, on the other hand, there will be no time-out at all. since the time-outs occur from the por pulse, if mclr is kept low long enough, all time-outs will expire, after which, bringing mclr high will allow program execution to begin immediately ( figure 23-5 ). this is useful for testing purposes or to synchronize more than one pic18f1xk50/pic18lf1xk50 device operating in parallel. table 23-2: time-out in various situations oscillator configuration power-up (2) and brown-out exit from power-managed mode pwrten = 0 pwrten = 1 hspll 66 ms (1) + 1024 t osc + 2 ms (2) 1024 t osc + 2 ms (2) 1024 t osc + 2 ms (2) hs, xt, lp 66 ms (1) + 1024 t osc 1024 t osc 1024 t osc ec, ecio 66 ms (1) ?? rc, rcio 66 ms (1) ?? intio1, intio2 66 ms (1) ?? note 1: 66 ms (65.5 ms) is the nominal power-up timer (pwrt) delay. 2: 2 ms is the nominal time required for the pll to lock.
pic18f/lf1xk50 ds41350e-page 282 preliminary ? 2010 microchip technology inc. figure 23-3: time-out sequ ence on power-up (mclr tied to v dd , v dd rise < t pwrt ) figure 23-4: time-out sequ ence on power-up (mclr not tied to v dd ): case 1 figure 23-5: time-out sequ ence on power-up (mclr not tied to v dd ): case 2 t pwrt t ost v dd mclr internal por pwrt time-out ost time-out internal reset t pwrt t ost v dd mclr internal por pwrt time-out ost time-out internal reset v dd mclr internal por pwrt time-out ost time-out internal reset t pwrt t ost
? 2010 microchip technology inc. preliminary ds41350e-page 283 pic18f/lf1xk50 figure 23-6: slow rise time (mclr tied to v dd , v dd rise > t pwrt ) figure 23-7: time-out sequence on por w/pll enabled (mclr tied to v dd ) v dd mclr internal por pwrt time-out ost time-out internal reset 0v 5v t pwrt t ost t pwrt t ost v dd mclr internal por pwrt time-out ost time-out internal reset pll time-out t pll note: t ost = 1024 clock cycles. t pll ? 2 ms max. first three stages of the pwrt timer.
pic18f/lf1xk50 ds41350e-page 284 preliminary ? 2010 microchip technology inc. 23.6 reset state of registers some registers are unaffected by a reset. their status is unknown on por and unchanged by all other resets. all other registers are forced to a ?reset state? depending on the type of reset that occurred. most registers are not affected by a wdt wake-up, since this is viewed as the resumption of normal operation. status bits from the rcon register, ri , to , pd , por and bor , are set or cleared differently in different reset situations, as indicated in tab l e 2 3- 3 . these bits are used by software to determine the nature of the reset. table 23-4 describes the reset states for all of the special function registers. these are categorized by power-on and brown-out resets, master clear and wdt resets and wdt wake-ups. table 23-3: status bits, their signifi cance and the initialization condition for rcon register condition program counter rcon register stkptr register sboren ri to pd por bor stkful stkunf power-on reset 0000h 1 11100 0 0 reset instruction 0000h u (2) 0uuuu u u brown-out reset 0000h u (2) 111u0 u u mclr during power-managed run modes 0000h u (2) u1uuu u u mclr during power-managed idle modes and sleep mode 0000h u (2) u10uu u u wdt time-out during full power or power-managed run mode 0000h u (2) u0uuu u u mclr during full power execution 0000h u (2) uuuuu u u stack full reset (stvren = 1 ) 0000h u (2) uuuuu 1 u stack underflow reset (stvren = 1 ) 0000h u (2) uuuuu u 1 stack underflow error (not an actual reset, stvren = 0 ) 0000h u (2) uuuuu u 1 wdt time-out during power-managed idle or sleep modes pc + 2 u (2) u00uu u u interrupt exit from power-managed modes pc + 2 (1) u (2) uu0uu u u legend: u = unchanged note 1: when the wake-up is due to an interrupt and the gieh or giel bits are set, the pc is loaded with the interrupt vector (008h or 0018h). 2: reset state is ? 1 ? for por and unchanged for all other resets when software bor is enabled (boren<1:0> configuration bits = 01 and sboren = 1 ). otherwise, the reset state is ? 0 ?.
? 2010 microchip technology inc. preliminary ds41350e-page 285 pic18f/lf1xk50 table 23-4: initialization conditions for all registers register address power-on reset, brown-out reset mclr resets, wdt reset, reset instruction, stack resets wake-up via wdt or interrupt tosu fffh ---0 0000 ---0 0000 ---0 uuuu (3) tosh ffeh 0000 0000 0000 0000 uuuu uuuu (3) tosl ffdh 0000 0000 0000 0000 uuuu uuuu (3) stkptr ffch 00-0 0000 uu-0 0000 uu-u uuuu (3) pclatu ffbh ---0 0000 ---0 0000 ---u uuuu pclath ffah 0000 0000 0000 0000 uuuu uuuu pcl ff9h 0000 0000 0000 0000 pc + 2 (2) tblptru ff8h ---0 0000 ---0 0000 ---u uuuu tblptrh ff7h 0000 0000 0000 0000 uuuu uuuu tblptrl ff6h 0000 0000 0000 0000 uuuu uuuu tablat ff5h 0000 0000 0000 0000 uuuu uuuu prodh ff4h xxxx xxxx uuuu uuuu uuuu uuuu prodl ff3h xxxx xxxx uuuu uuuu uuuu uuuu intcon ff2h 0000 000x 0000 000u uuuu uuuu (1) intcon2 ff1h 1111 -1-1 1111 -1-1 uuuu -u-u (1) intcon3 ff0h 11-0 0-00 11-0 0-00 uu-u u-uu (1) indf0 fefh n/a n/a n/a postinc0 feeh n/a n/a n/a postdec0 fedh n/a n/a n/a preinc0 fech n/a n/a n/a plusw0 febh n/a n/a n/a fsr0h feah ---- 0000 ---- 0000 ---- uuuu fsr0l fe9h xxxx xxxx uuuu uuuu uuuu uuuu wreg fe8h xxxx xxxx uuuu uuuu uuuu uuuu indf1 fe7h n/a n/a n/a postinc1 fe6h n/a n/a n/a postdec1 fe5h n/a n/a n/a preinc1 fe4h n/a n/a n/a plusw1 fe3h n/a n/a n/a legend: u = unchanged, x = unknown, - = unimplemented bit, read as ? 0 ?, q = value depends on condition. shaded cells indicate conditions do not apply for the designated device. note 1: one or more bits in the intconx or pirx registers will be affected (to cause wake-up). 2: when the wake-up is due to an interrupt and the giel or gieh bit is set, the pc is loaded with the interrupt vector (0008h or 0018h). 3: when the wake-up is due to an interrupt and the giel or gieh bit is set, the tosu, tosh and tosl are updated with the current value of the pc. the stkptr is modified to point to the next location in the hard- ware stack. 4: see table 23-3 for reset value for specific condition. 5: all bits of the anselh register initialize to ? 0 ? if the pbaden bit of config3h is ? 0 ?.
pic18f/lf1xk50 ds41350e-page 286 preliminary ? 2010 microchip technology inc. fsr1h fe2h ---- 0000 ---- 0000 ---- uuuu fsr1l fe1h xxxx xxxx uuuu uuuu uuuu uuuu bsr fe0h ---- 0000 ---- 0000 ---- uuuu indf2 fdfh n/a n/a n/a postinc2 fdeh n/a n/a n/a postdec2 fddh n/a n/a n/a preinc2 fdch n/a n/a n/a plusw2 fdbh n/a n/a n/a fsr2h fdah ---- 0000 ---- 0000 ---- uuuu fsr2l fd9h xxxx xxxx uuuu uuuu uuuu uuuu status fd8h ---x xxxx ---u uuuu ---u uuuu tmr0h fd7h 0000 0000 0000 0000 uuuu uuuu tmr0l fd6h xxxx xxxx uuuu uuuu uuuu uuuu t0con fd5h 1111 1111 1111 1111 uuuu uuuu osccon fd3h 0011 qq00 0011 qq00 uuuu uuuu osccon2 fd2h ---- -10x ---- -10x ---- -uuu wdtcon fd1h ---- ---0 ---- ---0 ---- ---u rcon (4) fd0h 0q-1 11q0 0q-q qquu uq-u qquu tmr1h fcfh xxxx xxxx uuuu uuuu uuuu uuuu tmr1l fceh xxxx xxxx uuuu uuuu uuuu uuuu t1con fcdh 0000 0000 u0uu uuuu uuuu uuuu tmr2 fcch 0000 0000 0000 0000 uuuu uuuu pr2 fcbh 1111 1111 1111 1111 1111 1111 t2con fcah -000 0000 -000 0000 -uuu uuuu sspbuf fc9h xxxx xxxx uuuu uuuu uuuu uuuu sspadd fc8h 0000 0000 0000 0000 uuuu uuuu sspstat fc7h 0000 0000 0000 0000 uuuu uuuu sspcon1 fc6h 0000 0000 0000 0000 uuuu uuuu sspcon2 fc5h 0000 0000 0000 0000 uuuu uuuu table 23-4: initialization conditions for all registers (continued) register address power-on reset, brown-out reset mclr resets, wdt reset, reset instruction, stack resets wake-up via wdt or interrupt legend: u = unchanged, x = unknown, - = unimplemented bit, read as ? 0 ?, q = value depends on condition. shaded cells indicate conditions do not apply for the designated device. note 1: one or more bits in the intconx or pirx registers will be affected (to cause wake-up). 2: when the wake-up is due to an interrupt and the giel or gieh bit is set, the pc is loaded with the interrupt vector (0008h or 0018h). 3: when the wake-up is due to an interrupt and the giel or gieh bit is set, the tosu, tosh and tosl are updated with the current value of the pc. the stkptr is modified to point to the next location in the hard- ware stack. 4: see table 23-3 for reset value for specific condition. 5: all bits of the anselh register initialize to ? 0 ? if the pbaden bit of config3h is ? 0 ?.
? 2010 microchip technology inc. preliminary ds41350e-page 287 pic18f/lf1xk50 adresh fc4h xxxx xxxx uuuu uuuu uuuu uuuu adresl fc3h xxxx xxxx uuuu uuuu uuuu uuuu adcon0 fc2h --00 0000 --00 0000 --uu uuuu adcon1 fc1h ---- 0000 ---- 0000 ---- uuuu adcon2 fc0h 0-00 0000 0-00 0000 u-uu uuuu ccpr1h fbfh xxxx xxxx uuuu uuuu uuuu uuuu ccpr1l fbeh xxxx xxxx uuuu uuuu uuuu uuuu ccp1con fbdh 0000 0000 0000 0000 uuuu uuuu refcon2 fbch ---0 0000 ---0 0000 ---u uuuu refcon1 fbbh 000- 00-0 000- 00-0 uuu- uu-u refcon0 fbah 0001 00-- 0001 00-- uuuu uu-- pstrcon fb9h ---0 0001 ---0 0001 ---u uuuu baudcon fb8h 0100 0-00 0100 0-00 uuuu u-uu pwm1con fb7h 0000 0000 0000 0000 uuuu uuuu eccp1as fb6h 0000 0000 0000 0000 uuuu uuuu tmr3h fb3h xxxx xxxx uuuu uuuu uuuu uuuu tmr3l fb2h xxxx xxxx uuuu uuuu uuuu uuuu t3con fb1h 0000 0000 uuuu uuuu uuuu uuuu spbrgh fb0h 0000 0000 0000 0000 uuuu uuuu spbrg fafh 0000 0000 0000 0000 uuuu uuuu rcreg faeh 0000 0000 0000 0000 uuuu uuuu txreg fadh 0000 0000 0000 0000 uuuu uuuu txsta fach 0000 0010 0000 0010 uuuu uuuu rcsta fabh 0000 000x 0000 000x uuuu uuuu eeadr faah 0000 0000 0000 0000 uuuu uuuu eedata fa8h 0000 0000 0000 0000 uuuu uuuu eecon2 fa7h 0000 0000 0000 0000 0000 0000 eecon1 fa6h xx-0 x000 uu-0 u000 uu-0 u000 table 23-4: initialization conditions for all registers (continued) register address power-on reset, brown-out reset mclr resets, wdt reset, reset instruction, stack resets wake-up via wdt or interrupt legend: u = unchanged, x = unknown, - = unimplemented bit, read as ? 0 ?, q = value depends on condition. shaded cells indicate conditions do not apply for the designated device. note 1: one or more bits in the intconx or pirx registers will be affected (to cause wake-up). 2: when the wake-up is due to an interrupt and the giel or gieh bit is set, the pc is loaded with the interrupt vector (0008h or 0018h). 3: when the wake-up is due to an interrupt and the giel or gieh bit is set, the tosu, tosh and tosl are updated with the current value of the pc. the stkptr is modified to point to the next location in the hard- ware stack. 4: see table 23-3 for reset value for specific condition. 5: all bits of the anselh register initialize to ? 0 ? if the pbaden bit of config3h is ? 0 ?.
pic18f/lf1xk50 ds41350e-page 288 preliminary ? 2010 microchip technology inc. ipr2 fa2h 1111 111- 1111 111- uuuu uuu- pir2 fa1h 0000 000- 0000 000- uuuu uuu- (1) pie2 fa0h 0000 000- 0000 000- uuuu uuu- ipr1 f9fh -111 1111 -111 1111 -uuu uuuu pir1 f9eh -000 0000 -000 0000 -uuu uuuu (1) pie1 f9dh -000 0000 -000 0000 -uuu uuuu osctune f9bh 0000 0000 0000 0000 uuuu uuuu trisc f95h 1111 1111 1111 1111 uuuu uuuu trisb f94h 1111 ---- 1111 ---- uuuu ---- trisa f93h --11 ---- --11 ---- --uu ---- latc f8bh xxxx xxxx uuuu uuuu uuuu uuuu latb f8ah xxxx ---- uuuu ---- uuuu ---- lata f89h --xx ---- --uu ---- --uu ---- portc f82h xxxx xxxx uuuu uuuu uuuu uuuu portb f81h xxxx ---- uuuu ---- uuuu ---- porta f80h --xx x-xx --xx x-xx --uu u-uu anselh (5) f7fh ---- 1111 ---- 1111 ---- uuuu ansel f7eh 1111 1--- 1111 1--- uuuu u--- iocb f7ah 0000 ---- 0000 ---- uuuu ---- ioca f79h --00 0-00 --00 0-00 --uu u-uu wpub f78h 1111 ---- 1111 ---- uuuu ---- wpua f77h --11 1--- --11 1--- --uu u--- slrcon f76h ---- -111 ---- -111 ---- -uuu sspmsk f6fh 1111 1111 1111 1111 uuuu uuuu cm1con0 f6dh 0000 0000 0000 0000 uuuu uuuu cm2con1 f6ch 0000 0000 0000 0000 uuuu uuuu cm2con0 f6bh 0000 0000 0000 0000 uuuu uuuu srcon1 f69h 0000 0000 0000 0000 uuuu uuuu srcon0 f68h 0000 0000 0000 0000 uuuu uuuu ucon f64h -0x0 000- -0x0 000- -uuu uuu- table 23-4: initialization conditions for all registers (continued) register address power-on reset, brown-out reset mclr resets, wdt reset, reset instruction, stack resets wake-up via wdt or interrupt legend: u = unchanged, x = unknown, - = unimplemented bit, read as ? 0 ?, q = value depends on condition. shaded cells indicate conditions do not apply for the designated device. note 1: one or more bits in the intconx or pirx registers will be affected (to cause wake-up). 2: when the wake-up is due to an interrupt and the giel or gieh bit is set, the pc is loaded with the interrupt vector (0008h or 0018h). 3: when the wake-up is due to an interrupt and the giel or gieh bit is set, the tosu, tosh and tosl are updated with the current value of the pc. the stkptr is modified to point to the next location in the hard- ware stack. 4: see table 23-3 for reset value for specific condition. 5: all bits of the anselh register initialize to ? 0 ? if the pbaden bit of config3h is ? 0 ?.
? 2010 microchip technology inc. preliminary ds41350e-page 289 pic18f/lf1xk50 ustat f63h -xxx xxx- -xxx xxx- -uuu uuu- uir f62h -000 0000 -000 0000 -uuu uuuu ucfg f61h 0--0 -000 0--0 -000 u--u -uuu uie f60h -000 0000 -000 0000 -uuu uuuu ueir f5fh 0--0 0000 0--0 0000 u--u uuuu ufrmh f5eh ---- -xxx ---- -xxx ---- -uuu ufrml f5dh xxxx xxxx xxxx xxxx uuuu uuuu uaddr f5ch -000 0000 -000 0000 -uuu uuuu ueie f5bh 0--0 0000 0--0 0000 u--u uuuu uep7 f5ah ----0 0000 ----0 0000 ----u uuuu uep6 f59h ----0 0000 ----0 0000 ----u uuuu uep5 f58h ----0 0000 ----0 0000 ----u uuuu uep4 f57h ----0 0000 ----0 0000 ----u uuuu uep3 f56h ----0 0000 ----0 0000 ----u uuuu uep2 f55h ----0 0000 ----0 0000 ----u uuuu uep1 f54h ----0 0000 ----0 0000 ----u uuuu uep0 f53h ----0 0000 ----0 0000 ----u uuuu table 23-4: initialization conditions for all registers (continued) register address power-on reset, brown-out reset mclr resets, wdt reset, reset instruction, stack resets wake-up via wdt or interrupt legend: u = unchanged, x = unknown, - = unimplemented bit, read as ? 0 ?, q = value depends on condition. shaded cells indicate conditions do not apply for the designated device. note 1: one or more bits in the intconx or pirx registers will be affected (to cause wake-up). 2: when the wake-up is due to an interrupt and the giel or gieh bit is set, the pc is loaded with the interrupt vector (0008h or 0018h). 3: when the wake-up is due to an interrupt and the giel or gieh bit is set, the tosu, tosh and tosl are updated with the current value of the pc. the stkptr is modified to point to the next location in the hard- ware stack. 4: see table 23-3 for reset value for specific condition. 5: all bits of the anselh register initialize to ? 0 ? if the pbaden bit of config3h is ? 0 ?.
pic18f/lf1xk50 ds41350e-page 290 preliminary ? 2010 microchip technology inc. notes:
? 2010 microchip technology inc. preliminary ds41350e-page 291 pic18f/lf1xk50 24.0 special features of the cpu pic18f/lf1xk50 devices include several features intended to maximize reliability and minimize cost through elimination of external components. these are: ? oscillator selection ? resets: - power-on reset (por) - power-up timer (pwrt) - oscillator start-up timer (ost) - brown-out reset (bor) ? interrupts ? watchdog timer (wdt) ? code protection ? id locations ? in-circuit serial programming? the oscillator can be configured for the application depending on frequency, power, accuracy and cost. all of the options are discussed in detail in section 2.0 ?oscillator module? . a complete discussion of device resets and interrupts is available in previous sections of this data sheet. in addition to their power-up and oscillator start-up timers provided for resets, pic18f/lf1xk50 devices have a watchdog timer, which is either permanently enabled via the configuration bits or software controlled (if configured as disabled). the inclusion of an internal rc oscillator also provides the additional benefits of a fail-safe clock monitor (fscm) and two-speed start-up. fscm provides for background monitoring of the peripheral clock and automatic switchover in the event of its failure. two- speed start-up enables code to be executed almost immediately on start-up, while the primary clock source completes its start-up delays. all of these features are enabled and configured by setting the appropriate configuration register bits.
pic18f/lf1xk50 ds41350e-page 292 preliminary ? 2010 microchip technology inc. 24.1 configuration bits the configuration bits can be programmed (read as ? 0 ?) or left unprogrammed (read as ? 1 ?) to select various device configurations. these bits are mapped starting at program memory location 300000h. the user will note that address 300000h is beyond the user program memory space. in fact, it belongs to the configuration memory space (300000h-3fffffh), which can only be accessed using table reads and table writes. programming the configuration registers is done in a manner similar to programming the flash memory. the wr bit in the eecon1 register starts a self-timed write to the configuration register. in normal operation mode, a tblwt instruction with the tblptr pointing to the configuration register sets up the address and the data for the configuration register write. setting the wr bit starts a long write to the configuration register. the configuration registers are written a byte at a time. to write or erase a configuration cell, a tblwt instruction can write a ? 1 ? or a ? 0 ? into the cell. for additional details on flash programming, refer to section 4.5 ?writing to flash program memory? . table 24-1: configuration bits and device ids file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 default/ unprogrammed value 300000h config1l ? ? usbdiv cpudiv1 cpudiv0 ? ? ? --00 0--- 300001h config1h ieso fcmen pclken pllen fosc3 fosc2 fosc1 fosc0 0010 0111 300002h config2l ? ? ? borv1 borv0 boren1 boren0 pwrten ---1 1111 300003h config2h ? ? ? wdtps3 wdtps2 wdtps1 wdtps0 wdten ---1 1111 300005h config3h mclre ? ? ?hfofst ? ? ? 1--- 1--- 300006h config4l bkbug (2) enhcpu ? ?bbsizlvp ?stvren -0-- 01-1 300008h config5l ? ? ? ? ? ?cp1cp0 ---- --11 300009h config5h cpd cpb ? ? ? ? ? ? 11-- ---- 30000ah config6l ? ? ? ? ? ? wrt1 wrt0 ---- --11 30000bh config6h wrtd wrtb wrtc ? ? ? ? ? 111- ---- 30000ch config7l ? ? ? ? ? ? ebtr1 ebtr0 ---- --11 30000dh config7h ?ebtrb ? ? ? ? ? ? -1-- ---- 3ffffeh devid1 (1) dev2 dev1 dev0 rev4 rev3 rev2 rev1 rev0 qqqq qqqq (1) 3fffffh devid2 (1) dev10 dev9 dev8 dev7 dev6 dev5 dev4 dev3 0000 1100 legend: x = unknown, u = unchanged, ? = unimplemented, q = value depends on condition. shaded cells are unimplemented, read as ? 0 ? note 1: see register 24-13 for devid1 values. devid registers are read-only and cannot be programmed by the user. 2: bkbug is only used for the icd device. otherwise , this bit is unimplemented and reads as ? 1 ?.
? 2010 microchip technology inc. preliminary ds41350e-page 293 pic18f/lf1xk50 register 24-1: config1l: co nfiguration register 1 low u-0 u-0 r/p-0 r/p-0 r/p-0 u-0 u-0 u-0 ? ? usbdiv cpudiv1 cpudiv0 ? ? ? bit 7 bit 0 legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed x = bit is unknown bit 7-6 unimplemented: read as ? 0 ? bit 5 usbdiv: usb clock selection bit selects the clock source for low-speed usb operation 1 = usb clock comes from the osc1/osc2 divided by 2 0 = usb clock comes directly from the osc1/osc2 oscillator block; no divide bit 4-3 cpudiv<1:0>: cpu system clock selection bits 11 = cpu system clock divided by 4 10 = cpu system clock divided by 3 01 = cpu system clock divided by 2 00 = no cpu system clock divide bit 2-0 unimplemented: read as ? 0 ?
pic18f/lf1xk50 ds41350e-page 294 preliminary ? 2010 microchip technology inc. register 24-2: config1h: co nfiguration register 1 high r/p-0 r/p-0 r/p-1 r/p-0 r/p-0 r/p-1 r/p-1 r/p-1 ieso fcmen pclken pllen fosc3 fosc2 fosc1 fosc0 bit 7 bit 0 legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed x = bit is unknown bit 7 ieso: internal/external oscillator switchover bit 1 = oscillator switchover mode enabled 0 = oscillator switchover mode disabled bit 6 fcmen: fail-safe clock monitor enable bit 1 = fail-safe clock monitor enabled 0 = fail-safe clock monitor disabled bit 5 pclken: primary clock enable bit 1 = primary clock enabled 0 = primary clock is under software control bit 4 pllen: 4 x pll enable bit 1 = oscillator multiplied by 4 0 = pll is under software control bit 3-0 fosc<3:0>: oscillator selection bits 1111 = external rc oscillator, clkout function on osc2 1110 = external rc oscillator, clkout function on osc2 1101 = ec (low) 1100 = ec, clkout function on osc2 (low) 1011 = ec (medium) 1010 = ec, clkout function on osc2 (medium) 1001 = internal rc oscillator, clkout function on osc2 1000 = internal rc oscillator 0111 = external rc oscillator 0110 = external rc oscillator, clkout function on osc2 0101 =ec (high) 0100 = ec, clkout function on osc2 (high) 0011 = external rc oscillator, clkout function on osc2 0010 = hs oscillator 0001 = xt oscillator 0000 = lp oscillator
? 2010 microchip technology inc. preliminary ds41350e-page 295 pic18f/lf1xk50 register 24-3: config2l: co nfiguration register 2 low u-0 u-0 u-0 r/p-1 r/p-1 r/p-1 r/p-1 r/p-1 ? ? ?borv1 (1) borv0 (1) boren1 (2) boren0 (2) pwrten (2) bit 7 bit 0 legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed x = bit is unknown bit 7-5 unimplemented: read as ? 0 ? bit 4-3 borv<1:0>: brown-out reset voltage bits (1) 11 = v bor set to 1.9v nominal 10 = v bor set to 2.2v nominal 01 = v bor set to 2.7v nominal 00 = v bor set to 3.0v nominal bit 2-1 boren<1:0>: brown-out reset enable bits (2) 11 = brown-out reset enabled in hardware only (sboren is disabled) 10 = brown-out reset enabled in hardware only and disabled in sleep mode (sboren is disabled) 01 = brown-out reset enabled and controlled by software (sboren is enabled) 00 = brown-out reset disabled in hardware and software bit 0 pwrten : power-up timer enable bit (2) 1 = pwrt disabled 0 = pwrt enabled note 1: see table 27-5 for specifications. 2: the power-up timer is decoupled from brown-out reset, allowing these features to be independently controlled.
pic18f/lf1xk50 ds41350e-page 296 preliminary ? 2010 microchip technology inc. register 24-4: config2h: co nfiguration register 2 high u-0 u-0 u-0 r/p-1 r/p-1 r/p-1 r/p-1 r/p-1 ? ? ? wdtps3 wdtps2 wdtps1 wdtps0 wdten bit 7 bit 0 legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed x = bit is unknown bit 7-5 unimplemented: read as ? 0 ? bit 4-1 wdtps<3:0>: watchdog timer postscale select bits 1111 = 1:32,768 1110 = 1:16,384 1101 = 1:8,192 1100 = 1:4,096 1011 = 1:2,048 1010 = 1:1,024 1001 = 1:512 1000 = 1:256 0111 = 1:128 0110 = 1:64 0101 = 1:32 0100 = 1:16 0011 = 1:8 0010 = 1:4 0001 = 1:2 0000 = 1:1 bit 0 wdten: watchdog timer enable bit 1 = wdt is always enabled. swdten bit has no effect 0 = wdt is controlled by swdten bit of the wdtcon register
? 2010 microchip technology inc. preliminary ds41350e-page 297 pic18f/lf1xk50 register 24-5: config3h: co nfiguration register 3 high r/p-1 u-0 u-0 u-0 r/p-1 u-0 u-0 u-0 mclre ? ? ? hfofst ? ? ? bit 7 bit 0 legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed x = bit is unknown bit 7 mclre: mclr pin enable bit 1 = mclr pin enabled; ra3 input pin disabled 0 = ra3 input pin enabled; mclr disabled bit 6-4 unimplemented: read as ? 0 ? bit 3 hfofst: hfintosc fast start-up bit 1 = hfintosc starts clocking the cpu without waiting for the oscillator to stabilize. 0 = the system clock is held off until the hfintosc is stable. bit 2-0 unimplemented: read as ? 0 ? register 24-6: config4l: co nfiguration register 4 low r/w-1 (1) r/w-0 u-0 u-0 r/p-0 r/p-1 u-0 r/p-1 bkbug enhcpu ? ? bbsiz lvp ?stvren bit 7 bit 0 legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed x = bit is unknown bit 7 bkbug : background debugger enable bit (1) 1 = background debugger disabled 0 = background debugger functions enabled bit 6 enhcpu: enhanced cpu enable bit 1 = enhanced cpu enabled 0 = enhanced cpu disabled bit 5-4 unimplemented: read as ? 0 ? bit 3 bbsiz: boot block size select bit 1 = 2 kw boot block size for pic18f14k50/pic18lf14k50 (1 kw boot block size for pic18f13k50/pic18lf13k50) 0 = 1 kw boot block size for pic18f14k50/pic18lf14k50 (512 w boot block size for pic18f13k50/pic18lf13k50) bit 2 lvp: single-supply icsp? enable bit 1 = single-supply icsp enabled 0 = single-supply icsp disabled bit 1 unimplemented: read as ? 0 ? bit 0 stvren: stack full/underflow reset enable bit 1 = stack full/underflow will cause reset 0 = stack full/underflow will not cause reset note 1: bkbug is only used for the icd device. otherwise, this bit is unimplemented and reads as ? 1 ?.
pic18f/lf1xk50 ds41350e-page 298 preliminary ? 2010 microchip technology inc. register 24-7: config5l: co nfiguration register 5 low u-0 u-0 u-0 u-0 u-0 u-0 r/c-1 r/c-1 ? ? ? ? ? ?cp1cp0 bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed c = clearable only bit bit 7-2 unimplemented: read as ? 0 ? bit 1 cp1: code protection bit 1 = block 1 not code-protected 0 = block 1 code-protected bit 0 cp0: code protection bit 1 = block 0 not code-protected 0 = block 0 code-protected register 24-8: config5h: co nfiguration register 5 high r/c-1 r/c-1 u-0 u-0 u-0 u-0 u-0 u-0 cpd cpb ? ? ? ? ? ? bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed c = clearable only bit bit 7 cpd: data eeprom code protection bit 1 = data eeprom not code-protected 0 = data eeprom code-protected bit 6 cpb: boot block code protection bit 1 = boot block not code-protected 0 = boot block code-protected bit 5-0 unimplemented: read as ? 0 ?
? 2010 microchip technology inc. preliminary ds41350e-page 299 pic18f/lf1xk50 register 24-9: config6l: co nfiguration register 6 low u-0 u-0 u-0 u-0 u-0 u-0 r/c-1 r/c-1 ? ? ? ? ? ?wrt1wrt0 bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed c = clearable only bit bit 7-2 unimplemented: read as ? 0 ? bit 1 wrt1: write protection bit 1 = block 1 not write-protected 0 = block 1 write-protected bit 0 wrt0: write protection bit 1 = block 0 not write-protected 0 = block 0 write-protected register 24-10: config6h: co nfiguration register 6 high r/c-1 r/c-1 r-1 u-0 u-0 u-0 u-0 u-0 wrtd wrtb wrtc (1) ? ? ? ? ? bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed c = clearable only bit bit 7 wrtd: data eeprom write protection bit 1 = data eeprom not write-protected 0 = data eeprom write-protected bit 6 wrtb: boot block write protection bit 1 = boot block not write-protected 0 = boot block write-protected bit 5 wrtc: configuration register write protection bit (1) 1 = configuration registers not write-protected 0 = configuration registers write-protected bit 4-0 unimplemented: read as ? 0 ? note 1: this bit is read-only in normal execution mode; it can be written only in program mode.
pic18f/lf1xk50 ds41350e-page 300 preliminary ? 2010 microchip technology inc. register 24-11: config7l: co nfiguration register 7 low u-0 u-0 u-0 u-0 u-0 u-0 r/c-1 r/c-1 ? ? ? ? ? ? ebtr1 ebtr0 bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed c = clearable only bit bit 7-2 unimplemented: read as ? 0 ? bit 1 ebtr1: table read protection bit 1 = block 1 not protected from table reads executed in other blocks 0 = block 1 protected from table reads executed in other blocks bit 0 ebtr0: table read protection bit 1 = block 0 not protected from table reads executed in other blocks 0 = block 0 protected from table reads executed in other blocks register 24-12: config7h: co nfiguration register 7 high u-0 r/c-1 u-0 u-0 u-0 u-0 u-0 u-0 ? ebtrb ? ? ? ? ? ? bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed c = clearable only bit bit 7 unimplemented: read as ? 0 ? bit 6 ebtrb: boot block table read protection bit 1 = boot block not protected from table reads executed in other blocks 0 = boot block protected from table reads executed in other blocks bit 5-0 unimplemented: read as ? 0 ?
? 2010 microchip technology inc. preliminary ds41350e-page 301 pic18f/lf1xk50 register 24-13: devid1: device id register 1 for pic18f1xk50/pic18lf1xk50 rrrrrrrr dev2 dev1 dev0 rev4 rev3 rev2 rev1 rev0 bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed c = clearable only bit bit 7-5 dev<2:0>: device id bits 010 = pic18f13k50 011 = pic18f14k50 bit 4-0 rev<4:0>: revision id bits these bits are used to indicate the device revision. register 24-14: devid2: device id register 2 for pic18f1xk50/pic18lf1xk50 rrrrrrrr dev10 dev9 dev8 dev7 dev6 dev5 dev4 dev3 bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed c = clearable only bit bit 7-0 dev<10:3>: device id bits these bits are used with the dev<2:0> bits in the device id register 1 to identify the part number. 0010 0000 = pic18f1xk50/pic18lf1xk50 devices note 1: these values for dev<10:3> may be shared with other devices. the specific device is always identified by using the entire dev<10:0> bit sequence.
pic18f/lf1xk50 ds41350e-page 302 preliminary ? 2010 microchip technology inc. 24.2 watchdog timer (wdt) for pic18f/lf1xk50 devices, the wdt is driven by the lfintosc source. when the wdt is enabled, the clock source is also enabled. the nominal wdt period is 4 ms and has the same stability as the lfintosc oscillator. the 4 ms period of the wdt is multiplied by a 16-bit postscaler. any output of the wdt postscaler is selected by a multiplexer, controlled by bits in configu- ration register 2h. available periods range from 4 ms to 131.072 seconds (2.18 minutes). the wdt and postscaler are cleared when any of the following events occur: a sleep or clrwdt instruction is executed, the ircf bits of the osccon register are changed or a clock failure has occurred. figure 24-1: wdt block diagram note 1: the clrwdt and sleep instructions clear the wdt and postscaler counts when executed. 2: changing the setting of the ircf bits of the osccon register clears the wdt and postscaler counts. 3: when a clrwdt instruction is executed, the postscaler count will be cleared. lfintosc source wdt wake-up reset wdt counter programmable postscaler 1:1 to 1:32,768 enable wdt wdtps<3:0> swdten wdten clrwdt 4 from power reset all device resets sleep ? 128 change on ircf bits managed modes
? 2010 microchip technology inc. preliminary ds41350e-page 303 pic18f/lf1xk50 24.2.1 control register register 24-15 shows the wdtcon register. this is a readable and writable register which contains a control bit that allows software to override the wdt enable configuration bit, but only if the configuration bit has disabled the wdt. table 24-2: summary of watchdog timer registers 24.3 program verification and code protection the overall structure of the code protection on the pic18 flash devices differs significantly from other pic ? microcontroller devices. the user program memory is divided into five blocks. one of these is a boot block of 0.5k or 2k bytes, depending on the device. the remainder of the mem- ory is divided into individual blocks on binary boundar- ies. each of the five blocks has three code protection bits associated with them. they are: ? code-protect bit (cpn) ? write-protect bit (wrtn) ? external block table read bit (ebtrn) figure 24-2 shows the program memory organization for 8, 16 and 32-kbyte devices and the specific code protection bit associated with each block. the actual locations of the bits are summarized in ta b l e 2 4 - 3 . register 24-15: wdtcon: wat chdog timer control register u-0 u-0 u-0 u-0 u-0 u-0 u-0 r/w-0 ? ? ? ? ? ? ?swdten (1) bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-1 unimplemented: read as ? 0 ? bit 0 swdten: software enable or disable the watchdog timer bit (1) 1 = wdt is turned on 0 = wdt is turned off (reset value) note 1: this bit has no effect if the configuration bit, wdten, is enabled. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page rcon ipen sboren ? ri to pd por bor 278 wdtcon ? ? ? ? ? ? ?swdten286 config2h wdtps3 wdtps2 wdtps1 wdtps0 wdten 296 legend: ? = unimplemented, read as ? 0 ?. shaded cells are not used by the watchdog timer.
pic18f/lf1xk50 ds41350e-page 304 preliminary ? 2010 microchip technology inc. figure 24-2: code-protected prog ram memory for pic18f/lf1xk50 device address (from/to) 14k50 13k50 bbsiz = 1 bbsiz = 0 bbsiz = 1 bbsiz = 0 0000h 01ffh boot block, 2 kw cpb, wrtb, ebtrb boot block, 1 kw cpb, wrtb, ebtrb boot block, 1 kw cpb, wrtb, ebtrb boot block, 0.512 kw cpb, wrtb, ebtrb 0200h 03ffh block 0 1.512 kw cp0, wrt0, ebtr0 0400h 05ffh block 0 3 kw cp0, wrt0, ebtr0 block 0 1 kw cp0, wrt0, ebtr0 0600h 07ffh 0800h 0fffh block 0 2 kw cp0, wrt0, ebtr0 block 1 2 kw cp1, wrt1, ebtr1 block 1 2 kw cp1, wrt1, ebtr1 1000h 1fffh block 1 4 kw cp1, wrt1, ebtr1 block 1 4 kw cp1, wrt1, ebtr1 reads all ? 0 ?s reads all ? 0 ?s 2000h 27ffh reads all ? 0 ?s reads all ? 0 ?s 2800h 2fffh 3000h 37ffh 3800h 3fffh 4000h 47ffh 4800h 4fffh 5000h 57ffh 5800h 5fffh 6000h 67ffh 6800h 6fffh 7000h 77ffh 7800h 7fffh 8000h ffffh note: refer to the test section for requirements on test memory mapping.
? 2010 microchip technology inc. preliminary ds41350e-page 305 pic18f/lf1xk50 table 24-3: summary of code protection registers 24.3.1 program memory code protection the program memory may be read to or written from any location using the table read and table write instructions. the device id may be read with table reads. the configuration registers may be read and written with the table read and table write instructions. in normal execution mode, the cpn bits have no direct effect. cpn bits inhibit external reads and writes. a block of user memory may be protected from table writes if the wrtn configuration bit is ? 0 ?. the ebtrn bits control table reads. for a block of user memory with the ebtrn bit cleared to ? 0 ?, a table read instruction that executes from within that block is allowed to read. a table read instruction that executes from a location outside of that block is not allowed to read and will result in reading ? 0 ?s. figures 24-3 through 24-5 illustrate table write and table read protection. figure 24-3: table write (wrtn) disallowed file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 300008h config5l ? ? ? ? ? ?cp1cp0 300009h config5h cpd cpb ? ? ? ? ? ? 30000ah config6l ? ? ? ? ? ? wrt1 wrt0 30000bh config6h wrtd wrtb wrtc ? ? ? ? ? 30000ch config7l ? ? ? ? ? ? ebtr1 ebtr0 30000dh config7h ? ebtrb ? ? ? ? ? ? legend: shaded cells are unimplemented. note: code protection bits may only be written to a ? 0 ? from a ? 1 ? state. it is not possible to write a ? 1 ? to a bit in the ? 0 ? state. code pro- tection bits are only set to ? 1 ? by a full chip erase or block erase function. the full chip erase and block erase functions can only be initiated via icsp or an external programmer. 000000h 0007ffh 000800h 001fffh 002000h 003fffh 004000h 005fffh 006000h 007fffh wrtb, ebtrb = 11 wrt0, ebtr0 = 01 wrt1, ebtr1 = 11 wrt2, ebtr2 = 11 wrt3, ebtr3 = 11 tblwt* tblptr = 0008ffh pc = 001ffeh tblwt* pc = 005ffeh register values program memory configuration bit settings results: all table writes disabled to blockn whenever wrtn = 0 .
pic18f/lf1xk50 ds41350e-page 306 preliminary ? 2010 microchip technology inc. figure 24-4: external block table read (ebtrn) disallowed figure 24-5: external block table read (ebtrn) allowed wrtb, ebtrb = 11 wrt0, ebtr0 = 10 wrt1, ebtr1 = 11 wrt2, ebtr2 = 11 wrt3, ebtr3 = 11 tblrd* tblptr = 0008ffh pc = 003ffeh results: all table reads from external blocks to blockn are disabled whenever ebtrn = 0 . tablat register returns a value of ? 0 ?. register values program memory configuration bit settings 000000h 0007ffh 000800h 001fffh 002000h 003fffh 004000h 005fffh 006000h 007fffh wrtb, ebtrb = 11 wrt0, ebtr0 = 10 wrt1, ebtr1 = 11 wrt2, ebtr2 = 11 wrt3, ebtr3 = 11 tblrd* tblptr = 0008ffh pc = 001ffeh register values program memory configuration bit settings results: table reads permitted within blockn, even when ebtrbn = 0 . tablat register returns the value of the data at the location tblptr. 000000h 0007ffh 000800h 001fffh 002000h 003fffh 004000h 005fffh 006000h 007fffh
? 2010 microchip technology inc. preliminary ds41350e-page 307 pic18f/lf1xk50 24.3.2 data eeprom code protection the entire data eeprom is protected from external reads and writes by two bits: cpd and wrtd. cpd inhibits external reads and writes of data eeprom. wrtd inhibits internal and external writes to data eeprom. the cpu can always read data eeprom under normal operation, regardless of the protection bit settings. 24.3.3 configuration register protection the configuration registers can be write-protected. the wrtc bit controls protection of the configuration registers. in normal execution mode, the wrtc bit is readable only. wrtc can only be written via icsp or an external programmer. 24.4 id locations eight memory locations (200000h-200007h) are designated as id locations, where the user can store checksum or other code identification numbers. these locations are both readable and writable during normal execution through the tblrd and tblwt instructions or during program/verify. the id locations can be read when the device is code-protected. 24.5 in-circuit serial programming pic18f/lf1xk50 devices can be serially programmed while in the end application circuit. this is simply done with two lines for clock and data and three other lines for power, ground and the programming voltage. this allows customers to manufacture boards with unprogrammed devices and then program the microcontroller just before shipping the product. this also allows the most recent firmware or a custom firmware to be programmed. 24.6 in-circuit debugger when the debug configuration bit is programmed to a ? 0 ?, the in-circuit debugger functionality is enabled. this function allows simple debugging functions when used with mplab ? ide. when the microcontroller has this feature enabled, some resources are not available for general use. table 24-4 shows which resources are required by the background debugger. table 24-4: debugger resources to use the in-circuit debugger function of the microcontroller, the design must implement in-circuit serial programming connections to the following pins: ?mclr /v pp /ra3 ?v dd ?v ss ?ra0 ?ra1 this will interface to the in-circuit debugger module available from microchip or one of the third party development tool companies. 24.7 single-supply icsp programming the lvp configuration bit enables single-supply icsp programming (formerly known as low-voltage icsp programming or lvp). when single-supply programming is enabled, the microcontroller can be programmed without requiring high voltage being applied to the mclr /v pp /ra3 pin, but the rc3/pgm pin is then dedicated to controlling program mode entry and is not available as a general purpose i/o pin. while programming, using single-supply programming mode, v dd is applied to the mclr /v pp /ra3 pin as in normal execution mode. to enter programming mode, v dd is applied to the pgm pin. if single-supply icsp programming mode will not be used, the lvp bit can be cleared. rc3/pgm then becomes available as the digital i/o pin, rc3. the lvp bit may be set or cleared only when using standard high-voltage programming (v ihh applied to the mclr / v pp /ra3 pin). once lvp has been disabled, only the standard high-voltage programming is available and must be used to program the device. memory that is not code-protected can be erased using either a block erase, or erased row by row, then written at any specified v dd . if code-protected memory is to be erased, a block erase is required. i/o pins: ra0, ra1 stack: 2 levels program memory: 512 bytes data memory: 10 bytes note 1: high-voltage programming is always available, regardless of the state of the lvp bit or the pgm pin, by applying v ihh to the mclr pin. 2: by default, single-supply icsp is enabled in unprogrammed devices (as supplied from microchip) and erased devices. 3: when single-supply programming is enabled, the rc3 pin can no longer be used as a general purpose i/o pin. 4: when lvp is enabled, externally pull the pgm pin to v ss to allow normal program execution.
pic18f/lf1xk50 ds41350e-page 308 preliminary ? 2010 microchip technology inc. notes:
? 2010 microchip technology inc. preliminary ds41350e-page 309 pic18f/lf1xk50 25.0 instruction set summary pic18f/lf1xk50 devices incorporate the standard set of 75 pic18 core instructions, as well as an extended set of 8 new instructions, for the optimization of code that is recursive or that utilizes a software stack. the extended set is discussed later in this section. 25.1 standard instruction set the standard pic18 instruction set adds many enhancements to the previous pic ? mcu instruction sets, while maintaining an easy migration from these pic ? mcu instruction sets. most instructions are a sin- gle program memory word (16 bits), but there are four instructions that require two program memory loca- tions. each single-word instruction is a 16-bit word divided into an opcode, which specifies the instruction type and one or more operands, which further specify the operation of the instruction. the instruction set is highly orthogonal and is grouped into four basic categories: ? byte-oriented operations ? bit-oriented operations ? literal operations ? control operations the pic18 instruction set summary in table 25-2 lists byte-oriented , bit-oriented , literal and control operations. table 25-1 shows the opcode field descriptions. most byte-oriented instructions have three operands: 1. the file register (specified by ?f?) 2. the destination of the result (specified by ?d?) 3. the accessed memory (specified by ?a?) the file register designator ?f? specifies which file register is to be used by the instruction. the destination designator ?d? specifies where the result of the opera- tion is to be placed. if ?d? is zero, the result is placed in the wreg register. if ?d? is one, the result is placed in the file register specified in the instruction. all bit-oriented instructions have three operands: 1. the file register (specified by ?f?) 2. the bit in the file register (specified by ?b?) 3. the accessed memory (specified by ?a?) the bit field designator ?b? selects the number of the bit affected by the operation, while the file register designator ?f? represents the number of the file in which the bit is located. the literal instructions may use some of the following operands: ? a literal value to be loaded into a file register (specified by ?k?) ? the desired fsr register to load the literal value into (specified by ?f?) ? no operand required (specified by ???) the control instructions may use some of the following operands: ? a program memory address (specified by ?n?) ? the mode of the call or return instructions (specified by ?s?) ? the mode of the table read and table write instructions (specified by ?m?) ? no operand required (specified by ???) all instructions are a single word, except for four double-word instructions. these instructions were made double-word to contain the required information in 32 bits. in the second word, the 4 msbs are ? 1 ?s. if this second word is executed as an instruction (by itself), it will execute as a nop . all single-word instructions are executed in a single instruction cycle, unless a conditional test is true or the program counter is changed as a result of the instruc- tion. in these cases, the execution takes two instruction cycles, with the additional instruction cycle(s) executed as a nop . the double-word instructions execute in two instruction cycles. one instruction cycle consists of four oscillator periods. thus, for an oscillator frequency of 4 mhz, the normal instruction execution time is 1 ? s. if a conditional test is true, or the program counter is changed as a result of an instruction, the instruction execution time is 2 ? s. two-word branch instructions (if true) would take 3 ? s. figure 25-1 shows the general formats that the instruc- tions can have. all examples use the convention ?nnh? to represent a hexadecimal number. the instruction set summary, shown in ta bl e 2 5 -2 , lists the standard instructions recognized by the microchip assembler (mpasm tm ). section 25.1.1 ?standard instruction set? provides a description of each instruction.
pic18f/lf1xk50 ds41350e-page 310 preliminary ? 2010 microchip technology inc. table 25-1: opcode field descriptions field description a ram access bit a = 0 : ram location in access ram (bsr register is ignored) a = 1 : ram bank is specified by bsr register bbb bit address within an 8-bit file register (0 to 7). bsr bank select register. used to select the current ram bank. c, dc, z, ov, n alu status bits: c arry, d igit c arry, z ero, ov erflow, n egative. d destination select bit d = 0 : store result in wreg d = 1 : store result in file register f dest destination: either the wreg register or the specified register file location. f 8-bit register file address (00h to ffh) or 2-bit fsr designator (0h to 3h). f s 12-bit register file address (000h to ff fh). this is the source address. f d 12-bit register file address (000h to fffh). this is the destination address. gie global interrupt enable bit. k literal field, constant data or label (may be either an 8-bit, 12-bit or a 20-bit value). label label name. mm the mode of the tblptr register for the table read and table write instructions. only used with table read and table write instructions: * no change to register (such as tb lptr with table reads and writes) *+ post-increment register (such as tblptr with table reads and writes) *- post-decrement register (such as tblptr with table reads and writes) +* pre-increment register (such as tblptr with table reads and writes) n the relative address (2?s complement number) for relative branch instructions or the direct address for call / branch and return instructions. pc program counter. pcl program counter low byte. pch program counter high byte. pclath program counter high byte latch. pclatu program counter upper byte latch. pd power-down bit. prodh product of multiply high byte. prodl product of multiply low byte. s fast call/return mode select bit s = 0 : do not update into/from shadow registers s = 1 : certain registers loaded into/from shadow registers (fast mode) tblptr 21-bit table pointer (points to a program memory location). tablat 8-bit table latch. to time-out bit. tos top-of-stack. u unused or unchanged. wdt watchdog timer. wreg working register (accumulator). x don?t care (? 0 ? or ? 1 ?). the assembler will generate code with x = 0 . it is the recommended form of use for compatibility with all microchip software tools. z s 7-bit offset value for indirect addr essing of register files (source). z d 7-bit offset value for indirect addressing of register files (destination). { } optional argument. [text] indicates an indexed address. (text) the contents of text . [expr] specifies bit n of the register indicated by the pointer expr . ? assigned to. < > register bit field. ? in the set of. italics user defined term (font is courier).
? 2010 microchip technology inc. preliminary ds41350e-page 311 pic18f/lf1xk50 figure 25-1: general format for instructions byte-oriented file register operations 15 10 9 8 7 0 d = 0 for result destination to be wreg register opcode d a f (file #) d = 1 for result destination to be file register (f) a = 0 to force access bank bit-oriented file register operations 15 12 11 9 8 7 0 opcode b (bit #) a f (file #) b = 3-bit position of bit in file register (f) literal operations 15 8 7 0 opcode k (literal) k = 8-bit immediate value byte to byte move operations (2-word) 15 12 11 0 opcode f (source file #) call , goto and branch operations 15 8 7 0 opcode n<7:0> (literal) n = 20-bit immediate value a = 1 for bsr to select bank f = 8-bit file register address a = 0 to force access bank a = 1 for bsr to select bank f = 8-bit file register address 15 12 11 0 1111 n<19:8> (literal) 15 12 11 0 1111 f (destination file #) f = 12-bit file register address control operations example instruction addwf myreg, w, b movff myreg1, myreg2 bsf myreg, bit, b movlw 7fh goto label 15 8 7 0 opcode n<7:0> (literal) 15 12 11 0 1111 n<19:8> (literal) call myfunc 15 11 10 0 opcode n<10:0> (literal) s = fast bit bra myfunc 15 8 7 0 opcode n<7:0> (literal) bc myfunc s
pic18f/lf1xk50 ds41350e-page 312 preliminary ? 2010 microchip technology inc. table 25-2: pic18fxxxx instruction set mnemonic, operands description cycles 16-bit instruction word status affected notes msb lsb byte-oriented operations addwf addwfc andwf clrf comf cpfseq cpfsgt cpfslt decf decfsz dcfsnz incf incfsz infsnz iorwf movf movff movwf mulwf negf rlcf rlncf rrcf rrncf setf subfwb subwf subwfb swapf tstfsz xorwf f, d, a f, d, a f, d, a f, a f, d, a f, a f, a f, a f, d, a f, d, a f, d, a f, d, a f, d, a f, d, a f, d, a f, d, a f s , f d f, a f, a f, a f, d, a f, d, a f, d, a f, d, a f, a f, d, a f, d, a f, d, a f, d, a f, a f, d, a add wreg and f add wreg and carry bit to f and wreg with f clear f complement f compare f with wreg, skip = compare f with wreg, skip > compare f with wreg, skip < decrement f decrement f, skip if 0 decrement f, skip if not 0 increment f increment f, skip if 0 increment f, skip if not 0 inclusive or wreg with f move f move f s (source) to 1st word f d (destination) 2nd word move wreg to f multiply wreg with f negate f rotate left f through carry rotate left f (no carry) rotate right f through carry rotate right f (no carry) set f subtract f from wreg with borrow subtract wreg from f subtract wreg from f with borrow swap nibbles in f test f, skip if 0 exclusive or wreg with f 1 1 1 1 1 1 (2 or 3) 1 (2 or 3) 1 (2 or 3) 1 1 (2 or 3) 1 (2 or 3) 1 1 (2 or 3) 1 (2 or 3) 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 (2 or 3) 1 0010 0010 0001 0110 0001 0110 0110 0110 0000 0010 0100 0010 0011 0100 0001 0101 1100 1111 0110 0000 0110 0011 0100 0011 0100 0110 0101 0101 0101 0011 0110 0001 01da0 0da 01da 101a 11da 001a 010a 000a 01da 11da 11da 10da 11da 10da 00da 00da ffff ffff 111a 001a 110a 01da 01da 00da 00da 100a 01da 11da 10da 10da 011a 10da ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff c, dc, z, ov, n c, dc, z, ov, n z, n z z, n none none none c, dc, z, ov, n none none c, dc, z, ov, n none none z, n z, n none none none c, dc, z, ov, n c, z, n z, n c, z, n z, n none c, dc, z, ov, n c, dc, z, ov, n c, dc, z, ov, n none none z, n 1, 2 1, 2 1,2 2 1, 2 4 4 1, 2 1, 2, 3, 4 1, 2, 3, 4 1, 2 1, 2, 3, 4 4 1, 2 1, 2 1 1, 2 1, 2 1, 2 1, 2 4 1, 2 note 1: when a port register is modified as a function of itself (e.g., movf portb, 1, 0 ), the value used will be that value present on the pins themselves. for example, if the data latch is ? 1 ? for a pin configured as i nput and is driven low by an external device, the data will be written back with a ? 0 ?. 2: if this instruction is executed on the tmr0 register (and where applicable, ?d? = 1 ), the prescaler will be cleared if assigned. 3: if program counter (pc) is modified or a conditional test is true, the instruction requires two cycles. the second cycle is executed as a nop . 4: some instructions are two-word instructions. the se cond word of these instructions will be executed as a nop unless the first word of the instruction retrieves the information embe dded in these 16 bits. this ensur es that all program memory locations have a valid instruction.
? 2010 microchip technology inc. preliminary ds41350e-page 313 pic18f/lf1xk50 bit-oriented operations bcf bsf btfsc btfss btg f, b, a f, b, a f, b, a f, b, a f, d, a bit clear f bit set f bit test f, skip if clear bit test f, skip if set bit toggle f 1 1 1 (2 or 3) 1 (2 or 3) 1 1001 1000 1011 1010 0111 bbba bbba bbba bbba bbba ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff none none none none none 1, 2 1, 2 3, 4 3, 4 1, 2 control operations bc bn bnc bnn bnov bnz bov bra bz call clrwdt daw goto nop nop pop push rcall reset retfie retlw return sleep n n n n n n n n n n, s ? ? n ? ? ? ? n s k s ? branch if carry branch if negative branch if not carry branch if not negative branch if not overflow branch if not zero branch if overflow branch unconditionally branch if zero call subroutine 1st word 2nd word clear watchdog timer decimal adjust wreg go to address 1st word 2nd word no operation no operation pop top of return stack (tos) push top of return stack (tos) relative call software device reset return from interrupt enable return with literal in wreg return from subroutine go into standby mode 1 (2) 1 (2) 1 (2) 1 (2) 1 (2) 1 (2) 1 (2) 2 1 (2) 2 1 1 2 1 1 1 1 2 1 2 2 2 1 1110 1110 1110 1110 1110 1110 1110 1101 1110 1110 1111 0000 0000 1110 1111 0000 1111 0000 0000 1101 0000 0000 0000 0000 0000 0010 0110 0011 0111 0101 0001 0100 0nnn 0000 110s kkkk 0000 0000 1111 kkkk 0000 xxxx 0000 0000 1nnn 0000 0000 1100 0000 0000 nnnn nnnn nnnn nnnn nnnn nnnn nnnn nnnn nnnn kkkk kkkk 0000 0000 kkkk kkkk 0000 xxxx 0000 0000 nnnn 1111 0001 kkkk 0001 0000 nnnn nnnn nnnn nnnn nnnn nnnn nnnn nnnn nnnn kkkk kkkk 0100 0111 kkkk kkkk 0000 xxxx 0110 0101 nnnn 1111 000s kkkk 001s 0011 none none none none none none none none none none to , pd c none none none none none none all gie/gieh, peie/giel none none to , pd 4 table 25-2: pic18fxxxx instruction set (continued) mnemonic, operands description cycles 16-bit instruction word status affected notes msb lsb note 1: when a port register is modified as a function of itself (e.g., movf portb, 1, 0 ), the value used will be that value present on the pins themselves. for example, if the data latch is ? 1 ? for a pin configured as i nput and is driven low by an external device, the data will be written back with a ? 0 ?. 2: if this instruction is executed on the tmr0 register (and where applicable, ?d? = 1 ), the prescaler will be cleared if assigned. 3: if program counter (pc) is modified or a conditional test is true, the instruction requires two cycles. the second cycle is executed as a nop . 4: some instructions are two-word instructions. the se cond word of these instructions will be executed as a nop unless the first word of the instruction retrieves the information embe dded in these 16 bits. this ensures that all program memory locations have a valid instruction.
pic18f/lf1xk50 ds41350e-page 314 preliminary ? 2010 microchip technology inc. literal operations addlw andlw iorlw lfsr movlb movlw mullw retlw sublw xorlw k k k f, k k k k k k k add literal and wreg and literal with wreg inclusive or literal with wreg move literal (12-bit) 2nd word to fsr(f) 1st word move literal to bsr<3:0> move literal to wreg multiply literal with wreg return with literal in wreg subtract wreg from literal exclusive or literal with wreg 1 1 1 2 1 1 1 2 1 1 0000 0000 0000 1110 1111 0000 0000 0000 0000 0000 0000 1111 1011 1001 1110 0000 0001 1110 1101 1100 1000 1010 kkkk kkkk kkkk 00ff kkkk 0000 kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk c, dc, z, ov, n z, n z, n none none none none none c, dc, z, ov, n z, n data memory ? program memory operations tblrd* tblrd*+ tblrd*- tblrd+* tblwt* tblwt*+ tblwt*- tblwt+* table read table read with post-increment table read with post-decrement table read with pre-increment table write table write with post-increment table write with post-decrement table write with pre-increment 2 2 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1000 1001 1010 1011 1100 1101 1110 1111 none none none none none none none none table 25-2: pic18fxxxx instruction set (continued) mnemonic, operands description cycles 16-bit instruction word status affected notes msb lsb note 1: when a port register is modified as a function of itself (e.g., movf portb, 1, 0 ), the value used will be that value present on the pins themselves. for example, if the data latch is ? 1 ? for a pin configured as i nput and is driven low by an external device, the data will be written back with a ? 0 ?. 2: if this instruction is executed on the tmr0 register (and where applicable, ?d? = 1 ), the prescaler will be cleared if assigned. 3: if program counter (pc) is modified or a conditional test is true, the instruction requires two cycles. the second cycle is executed as a nop . 4: some instructions are two-word instructions. the se cond word of these instructions will be executed as a nop unless the first word of the instruction retrieves the information embe dded in these 16 bits. this ensur es that all program memory locations have a valid instruction.
? 2010 microchip technology inc. preliminary ds41350e-page 315 pic18f/lf1xk50 25.1.1 standard instruction set addlw add literal to w syntax: addlw k operands: 0 ? k ? 255 operation: (w) + k ? w status affected: n, ov, c, dc, z encoding: 0000 1111 kkkk kkkk description: the contents of w are added to the 8-bit literal ?k? and the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to w example : addlw 15h before instruction w = 10h after instruction w = 25h addwf add w to f syntax: addwf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (w) + (f) ? dest status affected: n, ov, c, dc, z encoding: 0010 01da ffff ffff description: add w to register ?f?. if ?d? is ? 0 ?, the result is stored in w. if ?d? is ? 1 ?, the result is stored back in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : addwf reg, 0, 0 before instruction w = 17h reg = 0c2h after instruction w = 0d9h reg = 0c2h note: all pic18 instructions may take an optional label argument preceding the instruction mnemonic for use in symbolic addressing. if a label is used, the instruction format then becomes: {label} instruction argument(s).
pic18f/lf1xk50 ds41350e-page 316 preliminary ? 2010 microchip technology inc. addwfc add w and carry bit to f syntax: addwfc f {,d {,a}} operands: 0 ? f ? 255 d ?? [0,1] a ?? [0,1] operation: (w) + (f) + (c) ? dest status affected: n,ov, c, dc, z encoding: 0010 00da ffff ffff description: add w, the carry flag and data mem- ory location ?f?. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is placed in data memory location ?f?. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : addwfc reg, 0, 1 before instruction carry bit = 1 reg = 02h w=4dh after instruction carry bit = 0 reg = 02h w = 50h andlw and literal with w syntax: andlw k operands: 0 ? k ? 255 operation: (w) .and. k ? w status affected: n, z encoding: 0000 1011 kkkk kkkk description: the contents of w are and?ed with the 8-bit literal ?k?. the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to w example : andlw 05fh before instruction w=a3h after instruction w = 03h
? 2010 microchip technology inc. preliminary ds41350e-page 317 pic18f/lf1xk50 andwf and w with f syntax: andwf f {,d {,a}} operands: 0 ? f ? 255 d ?? [0,1] a ?? [0,1] operation: (w) .and. (f) ? dest status affected: n, z encoding: 0001 01da ffff ffff description: the contents of w are and?ed with register ?f?. if ?d? is ? 0 ?, the result is stored in w. if ?d? is ? 1 ?, the result is stored back in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : andwf reg, 0, 0 before instruction w = 17h reg = c2h after instruction w = 02h reg = c2h bc branch if carry syntax: bc n operands: -128 ? n ? 127 operation: if carry bit is ? 1 ? (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0010 nnnn nnnn description: if the carry bit is ? 1 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bc 5 before instruction pc = address (here) after instruction if carry = 1; pc = address (here + 12) if carry = 0; pc = address (here + 2)
pic18f/lf1xk50 ds41350e-page 318 preliminary ? 2010 microchip technology inc. bcf bit clear f syntax: bcf f, b {,a} operands: 0 ? f ? 255 0 ? b ? 7 a ?? [0,1] operation: 0 ? f status affected: none encoding: 1001 bbba ffff ffff description: bit ?b? in register ?f? is cleared. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write register ?f? example : bcf flag_reg, 7, 0 before instruction flag_reg = c7h after instruction flag_reg = 47h bn branch if negative syntax: bn n operands: -128 ? n ? 127 operation: if negative bit is ? 1 ? (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0110 nnnn nnnn description: if the negative bit is ? 1 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bn jump before instruction pc = address (here) after instruction if negative = 1; pc = address (jump) if negative = 0; pc = address (here + 2)
? 2010 microchip technology inc. preliminary ds41350e-page 319 pic18f/lf1xk50 bnc branch if not carry syntax: bnc n operands: -128 ? n ? 127 operation: if carry bit is ? 0 ? (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0011 nnnn nnnn description: if the carry bit is ? 0 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bnc jump before instruction pc = address (here) after instruction if carry = 0; pc = address (jump) if carry = 1; pc = address (here + 2) bnn branch if not negative syntax: bnn n operands: -128 ? n ? 127 operation: if negative bit is ? 0 ? (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0111 nnnn nnnn description: if the negative bit is ? 0 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bnn jump before instruction pc = address (here) after instruction if negative = 0; pc = address (jump) if negative = 1; pc = address (here + 2)
pic18f/lf1xk50 ds41350e-page 320 preliminary ? 2010 microchip technology inc. bnov branch if not overflow syntax: bnov n operands: -128 ? n ? 127 operation: if overflow bit is ? 0 ? (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0101 nnnn nnnn description: if the overflow bit is ? 0 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bnov jump before instruction pc = address (here) after instruction if overflow = 0; pc = address (jump) if overflow = 1; pc = address (here + 2) bnz branch if not zero syntax: bnz n operands: -128 ? n ? 127 operation: if zero bit is ? 0 ? (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0001 nnnn nnnn description: if the zero bit is ? 0 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bnz jump before instruction pc = address (here) after instruction if zero = 0; pc = address (jump) if zero = 1; pc = address (here + 2)
? 2010 microchip technology inc. preliminary ds41350e-page 321 pic18f/lf1xk50 bra unconditional branch syntax: bra n operands: -1024 ? n ? 1023 operation: (pc) + 2 + 2n ? pc status affected: none encoding: 1101 0nnn nnnn nnnn description: add the 2?s complement number ?2n? to the pc. since the pc will have incre- mented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is a two-cycle instruction. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation example : here bra jump before instruction pc = address (here) after instruction pc = address (jump) bsf bit set f syntax: bsf f, b {,a} operands: 0 ? f ? 255 0 ? b ? 7 a ?? [0,1] operation: 1 ? f status affected: none encoding: 1000 bbba ffff ffff description: bit ?b? in register ?f? is set. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write register ?f? example : bsf flag_reg, 7, 1 before instruction flag_reg = 0ah after instruction flag_reg = 8ah
pic18f/lf1xk50 ds41350e-page 322 preliminary ? 2010 microchip technology inc. btfsc bit test file, skip if clear syntax: btfsc f, b {,a} operands: 0 ? f ? 255 0 ? b ? 7 a ?? [0,1] operation: skip if (f) = 0 status affected: none encoding: 1011 bbba ffff ffff description: if bit ?b? in register ?f? is ? 0 ?, then the next instruction is skipped. if bit ?b? is ? 0 ?, then the next instruction fetched during the current instruction execution is discarded and a nop is executed instead, making this a two-cycle instruction. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here false true btfsc : : flag, 1, 0 before instruction pc = address (here) after instruction if flag<1> = 0; pc = address (true) if flag<1> = 1; pc = address (false) btfss bit test file, skip if set syntax: btfss f, b {,a} operands: 0 ? f ? 255 0 ? b < 7 a ?? [0,1] operation: skip if (f) = 1 status affected: none encoding: 1010 bbba ffff ffff description: if bit ?b? in register ?f? is ? 1 ?, then the next instruction is skipped. if bit ?b? is ? 1 ?, then the next instruction fetched during the current instruction execution is discarded and a nop is executed instead, making this a two-cycle instruction. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here false true btfss : : flag, 1, 0 before instruction pc = address (here) after instruction if flag<1> = 0; pc = address (false) if flag<1> = 1; pc = address (true)
? 2010 microchip technology inc. preliminary ds41350e-page 323 pic18f/lf1xk50 btg bit toggle f syntax: btg f, b {,a} operands: 0 ? f ? 255 0 ? b < 7 a ?? [0,1] operation: (f ) ? f status affected: none encoding: 0111 bbba ffff ffff description: bit ?b? in data memory location ?f? is inverted. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write register ?f? example : btg portc, 4, 0 before instruction: portc = 0111 0101 [75h] after instruction: portc = 0110 0101 [65h] bov branch if overflow syntax: bov n operands: -128 ? n ? 127 operation: if overflow bit is ? 1 ? (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0100 nnnn nnnn description: if the overflow bit is ? 1 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bov jump before instruction pc = address (here) after instruction if overflow = 1; pc = address (jump) if overflow = 0; pc = address (here + 2)
pic18f/lf1xk50 ds41350e-page 324 preliminary ? 2010 microchip technology inc. bz branch if zero syntax: bz n operands: -128 ? n ? 127 operation: if zero bit is ? 1 ? (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0000 nnnn nnnn description: if the zero bit is ? 1 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bz jump before instruction pc = address (here) after instruction if zero = 1; pc = address (jump) if zero = 0; pc = address (here + 2) call subroutine call syntax: call k {,s} operands: 0 ? k ? 1048575 s ?? [0,1] operation: (pc) + 4 ? tos, k ? pc<20:1>, if s = 1 (w) ? ws, (status) ? statuss, (bsr) ? bsrs status affected: none encoding: 1st word (k<7:0>) 2nd word(k<19:8>) 1110 1111 110s k 19 kkk k 7 kkk kkkk kkkk 0 kkkk 8 description: subroutine call of entire 2-mbyte memory range. first, return address (pc + 4) is pushed onto the return stack. if ?s? = 1 , the w, status and bsr registers are also pushed into their respective shadow registers, ws, statuss and bsrs. if ?s? = 0 , no update occurs (default). then, the 20-bit value ?k? is loaded into pc<20:1>. call is a two-cycle instruction. words: 2 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal ?k?<7:0>, push pc to stack read literal ?k?<19:8>, write to pc no operation no operation no operation no operation example : here call there, 1 before instruction pc = address (here) after instruction pc = address (there) tos = address (here + 4) ws = w bsrs = bsr statuss = status
? 2010 microchip technology inc. preliminary ds41350e-page 325 pic18f/lf1xk50 clrf clear f syntax: clrf f {,a} operands: 0 ? f ? 255 a ?? [0,1] operation: 000h ? f 1 ? z status affected: z encoding: 0110 101a ffff ffff description: clears the contents of the specified register. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write register ?f? example : clrf flag_reg, 1 before instruction flag_reg = 5ah after instruction flag_reg = 00h clrwdt clear watchdog timer syntax: clrwdt operands: none operation: 000h ? wdt, 000h ? wdt postscaler, 1 ? to, 1 ? pd status affected: to , pd encoding: 0000 0000 0000 0100 description: clrwdt instruction resets the watchdog timer. it also resets the post- scaler of the wdt. status bits, to and pd , are set. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode no operation process data no operation example : clrwdt before instruction wdt counter = ? after instruction wdt counter = 00h wdt postscaler = 0 to =1 pd =1
pic18f/lf1xk50 ds41350e-page 326 preliminary ? 2010 microchip technology inc. comf complement f syntax: comf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f ) ? dest status affected: n, z encoding: 0001 11da ffff ffff description: the contents of register ?f? are complemented. if ?d? is ? 0 ?, the result is stored in w. if ?d? is ? 1 ?, the result is stored back in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : comf reg, 0, 0 before instruction reg = 13h after instruction reg = 13h w=ech cpfseq compare f with w, skip if f = w syntax: cpfseq f {,a} operands: 0 ? f ? 255 a ? [0,1] operation: (f) ? (w), skip if (f) = (w) (unsigned comparison) status affected: none encoding: 0110 001a ffff ffff description: compares the contents of data memory location ?f? to the contents of w by performing an unsigned subtraction. if ?f? = w, then the fetched instruction is discarded and a nop is executed instead, making this a two-cycle instruction. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here cpfseq reg, 0 nequal : equal : before instruction pc address = here w=? reg = ? after instruction if reg = w; pc = address (equal) if reg ? w; pc = address (nequal)
? 2010 microchip technology inc. preliminary ds41350e-page 327 pic18f/lf1xk50 cpfsgt compare f with w, skip if f > w syntax: cpfsgt f {,a} operands: 0 ? f ? 255 a ? [0,1] operation: (f) ? ?? w), skip if (f) > (w) (unsigned comparison) status affected: none encoding: 0110 010a ffff ffff description: compares the contents of data memory location ?f? to the contents of the w by performing an unsigned subtraction. if the contents of ?f? are greater than the contents of wreg , then the fetched instruction is discarded and a nop is executed instead, making this a two-cycle instruction. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here cpfsgt reg, 0 ngreater : greater : before instruction pc = address (here) w= ? after instruction if reg ? w; pc = address (greater) if reg ? w; pc = address (ngreater) cpfslt compare f with w, skip if f < w syntax: cpfslt f {,a} operands: 0 ? f ? 255 a ? [0,1] operation: (f) ? ?? w), skip if (f) < (w) (unsigned comparison) status affected: none encoding: 0110 000a ffff ffff description: compares the contents of data memory location ?f? to the contents of w by performing an unsigned subtraction. if the contents of ?f? are less than the contents of w, then the fetched instruction is discarded and a nop is executed instead, making this a two-cycle instruction. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here cpfslt reg, 1 nless : less : before instruction pc = address (here) w= ? after instruction if reg < w; pc = address (less) if reg ? w; pc = address (nless)
pic18f/lf1xk50 ds41350e-page 328 preliminary ? 2010 microchip technology inc. daw decimal adjust w register syntax: daw operands: none operation: if [w<3:0> > 9] or [dc = 1] then (w<3:0>) + 6 ? w<3:0>; else ( w<3:0>) ? w<3:0>; if [w<7:4> + dc > 9] or [c = 1] then ( w<7:4>) + 6 + dc ? w<7:4> ? ; else (w<7:4>) + dc ? w<7:4> status affected: c encoding: 0000 0000 0000 0111 description: daw adjusts the eight-bit value in w, resulting from the earlier addition of two variables (each in packed bcd format) and produces a correct packed bcd result. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register w process data write w example1 : daw before instruction w=a5h c=0 dc = 0 after instruction w = 05h c=1 dc = 0 example 2 : before instruction w=ceh c=0 dc = 0 after instruction w = 34h c=1 dc = 0 decf decrement f syntax: decf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f) ? 1 ? dest status affected: c, dc, n, ov, z encoding: 0000 01da ffff ffff description: decrement register ?f?. if ?d? is ? 0 ?, the result is stored in w. if ?d? is ? 1 ?, the result is stored back in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : decf cnt, 1, 0 before instruction cnt = 01h z=0 after instruction cnt = 00h z=1
? 2010 microchip technology inc. preliminary ds41350e-page 329 pic18f/lf1xk50 decfsz decrement f, skip if 0 syntax: decfsz f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f) ? 1 ? dest, skip if result = 0 status affected: none encoding: 0010 11da ffff ffff description: the contents of register ?f? are decremented. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is placed back in register ?f? (default). if the result is ? 0 ?, the next instruction, which is already fetched, is discarded and a nop is executed instead, making it a two-cycle instruction. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here decfsz cnt, 1, 1 goto loop continue before instruction pc = address (here) after instruction cnt = cnt - 1 if cnt = 0; pc = address (continue) if cnt ? 0; pc = address (here + 2) dcfsnz decrement f, skip if not 0 syntax: dcfsnz f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f) ? 1 ? dest, skip if result ? 0 status affected: none encoding: 0100 11da ffff ffff description: the contents of register ?f? are decremented. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is placed back in register ?f? (default). if the result is not ? 0 ?, the next instruction, which is already fetched, is discarded and a nop is executed instead, making it a two-cycle instruction. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here dcfsnz temp, 1, 0 zero : nzero : before instruction temp = ? after instruction temp = temp ? 1, if temp = 0; pc = address (zero) if temp ? 0; pc = address (nzero)
pic18f/lf1xk50 ds41350e-page 330 preliminary ? 2010 microchip technology inc. goto unconditional branch syntax: goto k operands: 0 ? k ? 1048575 operation: k ? pc<20:1> status affected: none encoding: 1st word (k<7:0>) 2nd word(k<19:8>) 1110 1111 1111 k 19 kkk k 7 kkk kkkk kkkk 0 kkkk 8 description: goto allows an unconditional branch anywhere within entire 2-mbyte memory range. the 20-bit value ?k? is loaded into pc<20:1>. goto is always a two-cycle instruction. words: 2 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal ?k?<7:0>, no operation read literal ?k?<19:8>, write to pc no operation no operation no operation no operation example : goto there after instruction pc = address (there) incf increment f syntax: incf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f) + 1 ? dest status affected: c, dc, n, ov, z encoding: 0010 10da ffff ffff description: the contents of register ?f? are incremented. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is placed back in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : incf cnt, 1, 0 before instruction cnt = ffh z=0 c=? dc = ? after instruction cnt = 00h z=1 c=1 dc = 1
? 2010 microchip technology inc. preliminary ds41350e-page 331 pic18f/lf1xk50 incfsz increment f, skip if 0 syntax: incfsz f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f) + 1 ? dest, skip if result = 0 status affected: none encoding: 0011 11da ffff ffff description: the contents of register ?f? are incremented. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is placed back in register ?f? (default). if the result is ? 0 ?, the next instruction, which is already fetched, is discarded and a nop is executed instead, making it a two-cycle instruction. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here incfsz cnt, 1, 0 nzero : zero : before instruction pc = address (here) after instruction cnt = cnt + 1 if cnt = 0; pc = address (zero) if cnt ? 0; pc = address (nzero) infsnz increment f, skip if not 0 syntax: infsnz f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f) + 1 ? dest, skip if result ? 0 status affected: none encoding: 0100 10da ffff ffff description: the contents of register ?f? are incremented. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is placed back in register ?f? (default). if the result is not ? 0 ?, the next instruction, which is already fetched, is discarded and a nop is executed instead, making it a two-cycle instruction. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here infsnz reg, 1, 0 zero nzero before instruction pc = address (here) after instruction reg = reg + 1 if reg ? 0; pc = address (nzero) if reg = 0; pc = address (zero)
pic18f/lf1xk50 ds41350e-page 332 preliminary ? 2010 microchip technology inc. iorlw inclusive or literal with w syntax: iorlw k operands: 0 ? k ? 255 operation: (w) .or. k ? w status affected: n, z encoding: 0000 1001 kkkk kkkk description: the contents of w are ored with the eight-bit literal ?k?. the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to w example : iorlw 35h before instruction w=9ah after instruction w=bfh iorwf inclusive or w with f syntax: iorwf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (w) .or. (f) ? dest status affected: n, z encoding: 0001 00da ffff ffff description: inclusive or w with register ?f?. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is placed back in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : iorwf result, 0, 1 before instruction result = 13h w = 91h after instruction result = 13h w = 93h
? 2010 microchip technology inc. preliminary ds41350e-page 333 pic18f/lf1xk50 lfsr load fsr syntax: lfsr f, k operands: 0 ? f ? 2 0 ? k ? 4095 operation: k ? fsrf status affected: none encoding: 1110 1111 1110 0000 00ff k 7 kkk k 11 kkk kkkk description: the 12-bit literal ?k? is loaded into the file select register pointed to by ?f?. words: 2 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal ?k? msb process data write literal ?k? msb to fsrfh decode read literal ?k? lsb process data write literal ?k? to fsrfl example : lfsr 2, 3abh after instruction fsr2h = 03h fsr2l = abh movf move f syntax: movf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: f ? dest status affected: n, z encoding: 0101 00da ffff ffff description: the contents of register ?f? are moved to a destination dependent upon the status of ?d?. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is placed back in register ?f? (default). location ?f? can be anywhere in the 256-byte bank. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write w example : movf reg, 0, 0 before instruction reg = 22h w=ffh after instruction reg = 22h w = 22h
pic18f/lf1xk50 ds41350e-page 334 preliminary ? 2010 microchip technology inc. movff move f to f syntax: movff f s ,f d operands: 0 ? f s ? 4095 0 ? f d ? 4095 operation: (f s ) ? f d status affected: none encoding: 1st word (source) 2nd word (destin.) 1100 1111 ffff ffff ffff ffff fff f s fff f d description: the contents of source register ?f s ? are moved to destination register ?f d ?. location of source ?f s ? can be anywhere in the 4096-byte data space (000h to fffh) and location of destination ?f d ? can also be anywhere from 000h to fffh. either source or destination can be w (a useful special situation). movff is particularly useful for transferring a data memory location to a peripheral register (such as the transmit buffer or an i/o port). the movff instruction cannot use the pcl, tosu, tosh or tosl as the destination register. words: 2 cycles: 2 (3) q cycle activity: q1 q2 q3 q4 decode read register ?f? (src) process data no operation decode no operation no dummy read no operation write register ?f? (dest) example : movff reg1, reg2 before instruction reg1 = 33h reg2 = 11h after instruction reg1 = 33h reg2 = 33h movlb move literal to low nibble in bsr syntax: movlw k operands: 0 ? k ? 255 operation: k ? bsr status affected: none encoding: 0000 0001 kkkk kkkk description: the eight-bit literal ?k? is loaded into the bank select register (bsr). the value of bsr<7:4> always remains ? 0 ?, regardless of the value of k 7 :k 4 . words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write literal ?k? to bsr example : movlb 5 before instruction bsr register = 02h after instruction bsr register = 05h
? 2010 microchip technology inc. preliminary ds41350e-page 335 pic18f/lf1xk50 movlw move literal to w syntax: movlw k operands: 0 ? k ? 255 operation: k ? w status affected: none encoding: 0000 1110 kkkk kkkk description: the eight-bit literal ?k? is loaded into w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to w example : movlw 5ah after instruction w=5ah movwf move w to f syntax: movwf f {,a} operands: 0 ? f ? 255 a ? [0,1] operation: (w) ? f status affected: none encoding: 0110 111a ffff ffff description: move data from w to register ?f?. location ?f? can be anywhere in the 256-byte bank. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write register ?f? example : movwf reg, 0 before instruction w=4fh reg = ffh after instruction w=4fh reg = 4fh
pic18f/lf1xk50 ds41350e-page 336 preliminary ? 2010 microchip technology inc. mullw multiply literal with w syntax: mullw k operands: 0 ? k ? 255 operation: (w) x k ? prodh:prodl status affected: none encoding: 0000 1101 kkkk kkkk description: an unsigned multiplication is carried out between the contents of w and the 8-bit literal ?k?. the 16-bit result is placed in the prodh:prodl register pair. prodh contains the high byte. w is unchanged. none of the status flags are affected. note that neither overflow nor carry is possible in this operation. a zero result is possible but not detected. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write registers prodh: prodl example : mullw 0c4h before instruction w=e2h prodh = ? prodl = ? after instruction w=e2h prodh = adh prodl = 08h mulwf multiply w with f syntax: mulwf f {,a} operands: 0 ? f ? 255 a ? [0,1] operation: (w) x (f) ? prodh:prodl status affected: none encoding: 0000 001a ffff ffff description: an unsigned multiplication is carried out between the contents of w and the register file location ?f?. the 16-bit result is stored in the prodh:prodl register pair. prodh contains the high byte. both w and ?f? are unchanged. none of the status flags are affected. note that neither overflow nor carry is possible in this operation. a zero result is possible but not detected. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write registers prodh: prodl example : mulwf reg, 1 before instruction w=c4h reg = b5h prodh = ? prodl = ? after instruction w=c4h reg = b5h prodh = 8ah prodl = 94h
? 2010 microchip technology inc. preliminary ds41350e-page 337 pic18f/lf1xk50 negf negate f syntax: negf f {,a} operands: 0 ? f ? 255 a ? [0,1] operation: ( f ) + 1 ? f status affected: n, ov, c, dc, z encoding: 0110 110a ffff ffff description: location ?f? is negated using two?s complement. the result is placed in the data memory location ?f?. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write register ?f? example : negf reg, 1 before instruction reg = 0011 1010 [3ah] after instruction reg = 1100 0110 [c6h] nop no operation syntax: nop operands: none operation: no operation status affected: none encoding: 0000 1111 0000 xxxx 0000 xxxx 0000 xxxx description: no operation. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode no operation no operation no operation example : none.
pic18f/lf1xk50 ds41350e-page 338 preliminary ? 2010 microchip technology inc. pop pop top of return stack syntax: pop operands: none operation: (tos) ? bit bucket status affected: none encoding: 0000 0000 0000 0110 description: the tos value is pulled off the return stack and is discarded. the tos value then becomes the previous value that was pushed onto the return stack. this instruction is provided to enable the user to properly manage the return stack to incorporate a software stack. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode no operation pop tos value no operation example : pop goto new before instruction tos = 0031a2h stack (1 level down) = 014332h after instruction tos = 014332h pc = new push push top of return stack syntax: push operands: none operation: (pc + 2) ? tos status affected: none encoding: 0000 0000 0000 0101 description: the pc + 2 is pushed onto the top of the return stack. the previous tos value is pushed down on the stack. this instruction allows implementing a software stack by modifying tos and then pushing it onto the return stack. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode push pc + 2 onto return stack no operation no operation example : push before instruction tos = 345ah pc = 0124h after instruction pc = 0126h tos = 0126h stack (1 level down) = 345ah
? 2010 microchip technology inc. preliminary ds41350e-page 339 pic18f/lf1xk50 rcall relative call syntax: rcall n operands: -1024 ? n ? 1023 operation: (pc) + 2 ? tos, (pc) + 2 + 2n ? pc status affected: none encoding: 1101 1nnn nnnn nnnn description: subroutine call with a jump up to 1k from the current location. first, return address (pc + 2) is pushed onto the stack. then, add the 2?s complement number ?2n? to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is a two-cycle instruction. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal ?n? push pc to stack process data write to pc no operation no operation no operation no operation example : here rcall jump before instruction pc = address (here) after instruction pc = address (jump) tos = address (here + 2) reset reset syntax: reset operands: none operation: reset all registers and flags that are affected by a mclr reset. status affected: all encoding: 0000 0000 1111 1111 description: this instruction provides a way to execute a mclr reset by software. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode start reset no operation no operation example : reset after instruction registers = reset value flags* = reset value
pic18f/lf1xk50 ds41350e-page 340 preliminary ? 2010 microchip technology inc. retfie return from interrupt syntax: retfie {s} operands: s ? [0,1] operation: (tos) ? pc, 1 ? gie/gieh or peie/giel, if s = 1 (ws) ? w, (statuss) ? status, (bsrs) ? bsr, pclatu, pclath are unchanged. status affected: gie/gieh, peie/giel. encoding: 0000 0000 0001 000s description: return from interrupt. stack is popped and top-of-stack (tos) is loaded into the pc. interrupts are enabled by setting either the high or low priority global interrupt enable bit. if ?s? = 1 , the contents of the shadow registers, ws, statuss and bsrs, are loaded into their corresponding registers, w, status and bsr. if ?s? = 0 , no update of these registers occurs (default). words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode no operation no operation pop pc from stack set gieh or giel no operation no operation no operation no operation example : retfie 1 after interrupt pc = tos w=ws bsr = bsrs status = statuss gie/gieh, peie/giel = 1 retlw return literal to w syntax: retlw k operands: 0 ? k ? 255 operation: k ? w, (tos) ? pc, pclatu, pclath are unchanged status affected: none encoding: 0000 1100 kkkk kkkk description: w is loaded with the eight-bit literal ?k?. the program counter is loaded from the top of the stack (the return address). the high address latch (pclath) remains unchanged. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data pop pc from stack, write to w no operation no operation no operation no operation example : call table ; w contains table ; offset value ; w now has ; table value : table addwf pcl ; w = offset retlw k0 ; begin table retlw k1 ; : : retlw kn ; end of table before instruction w = 07h after instruction w = value of kn
? 2010 microchip technology inc. preliminary ds41350e-page 341 pic18f/lf1xk50 return return from subroutine syntax: return {s} operands: s ? [0,1] operation: (tos) ? pc, if s = 1 (ws) ? w, (statuss) ? status, (bsrs) ? bsr, pclatu, pclath are unchanged status affected: none encoding: 0000 0000 0001 001s description: return from subroutine. the stack is popped and the top of the stack (tos) is loaded into the program counter. if ?s?= 1 , the contents of the shadow registers, ws, statuss and bsrs, are loaded into their corresponding registers, w, status and bsr. if ?s? = 0 , no update of these registers occurs (default). words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode no operation process data pop pc from stack no operation no operation no operation no operation example : return after instruction: pc = tos rlcf rotate left f through carry syntax: rlcf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f) ? dest, (f<7>) ? c, (c) ? dest<0> status affected: c, n, z encoding: 0011 01da ffff ffff description: the contents of register ?f? are rotated one bit to the left through the carry flag. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is stored back in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : rlcf reg, 0, 0 before instruction reg = 1110 0110 c=0 after instruction reg = 1110 0110 w = 1100 1100 c=1 c register f
pic18f/lf1xk50 ds41350e-page 342 preliminary ? 2010 microchip technology inc. rlncf rotate left f (no carry) syntax: rlncf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f) ? dest, (f<7>) ? dest<0> status affected: n, z encoding: 0100 01da ffff ffff description: the contents of register ?f? are rotated one bit to the left. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is stored back in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : rlncf reg, 1, 0 before instruction reg = 1010 1011 after instruction reg = 0101 0111 register f rrcf rotate right f through carry syntax: rrcf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f) ? dest, (f<0>) ? c, (c) ? dest<7> status affected: c, n, z encoding: 0011 00da ffff ffff description: the contents of register ?f? are rotated one bit to the right through the carry flag. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is placed back in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : rrcf reg, 0, 0 before instruction reg = 1110 0110 c=0 after instruction reg = 1110 0110 w = 0111 0011 c=0 c register f
? 2010 microchip technology inc. preliminary ds41350e-page 343 pic18f/lf1xk50 rrncf rotate right f (no carry) syntax: rrncf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f) ? dest, (f<0>) ? dest<7> status affected: n, z encoding: 0100 00da ffff ffff description: the contents of register ?f? are rotated one bit to the right. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is placed back in register ?f? (default). if ?a? is ? 0 ?, the access bank will be selected, overriding the bsr value. if ?a? is ? 1 ?, then the bank will be selected as per the bsr value (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example 1 : rrncf reg, 1, 0 before instruction reg = 1101 0111 after instruction reg = 1110 1011 example 2 : rrncf reg, 0, 0 before instruction w=? reg = 1101 0111 after instruction w = 1110 1011 reg = 1101 0111 register f setf set f syntax: setf f {,a} operands: 0 ? f ? 255 a ?? [0,1] operation: ffh ? f status affected: none encoding: 0110 100a ffff ffff description: the contents of the specified register are set to ffh. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write register ?f? example : setf reg, 1 before instruction reg = 5ah after instruction reg = ffh
pic18f/lf1xk50 ds41350e-page 344 preliminary ? 2010 microchip technology inc. sleep enter sleep mode syntax: sleep operands: none operation: 00h ? wdt, 0 ? wdt postscaler, 1 ? to , 0 ? pd status affected: to , pd encoding: 0000 0000 0000 0011 description: the power-down status bit (pd ) is cleared. the time-out status bit (to ) is set. watchdog timer and its post- scaler are cleared. the processor is put into sleep mode with the oscillator stopped. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode no operation process data go to sleep example : sleep before instruction to =? pd =? after instruction to =1 ? pd =0 ? if wdt causes wake-up, this bit is cleared. subfwb subtract f from w with borrow syntax: subfwb f {,d {,a}} operands: 0 ?? f ?? 255 d ? [0,1] a ? [0,1] operation: (w) ? (f) ? (c ) ?? dest status affected: n, ov, c, dc, z encoding: 0101 01da ffff ffff description: subtract register ?f? and carry flag (borrow) from w (2?s complement method). if ?d? is ? 0 ?, the result is stored in w. if ?d? is ? 1 ?, the result is stored in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in index ed litera l offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example 1 : subfwb reg, 1, 0 before instruction reg = 3 w=2 c=1 after instruction reg = ff w=2 c=0 z=0 n = 1 ; result is negative example 2 : subfwb reg, 0, 0 before instruction reg = 2 w=5 c=1 after instruction reg = 2 w=3 c=1 z=0 n = 0 ; result is positive example 3 : subfwb reg, 1, 0 before instruction reg = 1 w=2 c=0 after instruction reg = 0 w=2 c=1 z = 1 ; result is zero n=0
? 2010 microchip technology inc. preliminary ds41350e-page 345 pic18f/lf1xk50 sublw subtract w from literal syntax: sublw k operands: 0 ?? k ?? 255 operation: k ? (w) ?? w status affected: n, ov, c, dc, z encoding: 0000 1000 kkkk kkkk description w is subtracted from the eight-bit literal ?k?. the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to w example 1: sublw 02h before instruction w = 01h c=? after instruction w = 01h c = 1 ; result is positive z=0 n=0 example 2 : sublw 02h before instruction w = 02h c=? after instruction w = 00h c = 1 ; result is zero z=1 n=0 example 3 : sublw 02h before instruction w = 03h c=? after instruction w = ffh ; (2?s complement) c = 0 ; result is negative z=0 n=1 subwf subtract w from f syntax: subwf f {,d {,a}} operands: 0 ?? f ?? 255 d ? [0,1] a ? [0,1] operation: (f) ? (w) ?? dest status affected: n, ov, c, dc, z encoding: 0101 11da ffff ffff description: subtract w from register ?f? (2?s complement method). if ?d? is ? 0 ?, the result is stored in w. if ?d? is ? 1 ?, the result is stored back in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in index ed litera l offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example 1 : subwf reg, 1, 0 before instruction reg = 3 w=2 c=? after instruction reg = 1 w=2 c = 1 ; result is positive z=0 n=0 example 2 : subwf reg, 0, 0 before instruction reg = 2 w=2 c=? after instruction reg = 2 w=0 c=1; result is zero z=1 n=0 example 3 : subwf reg, 1, 0 before instruction reg = 1 w=2 c=? after instruction reg = ffh ;(2?s complement) w=2 c = 0 ; result is negative z=0 n=1
pic18f/lf1xk50 ds41350e-page 346 preliminary ? 2010 microchip technology inc. subwfb subtract w from f with borrow syntax: subwfb f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f) ? (w) ? (c ) ?? dest status affected: n, ov, c, dc, z encoding: 0101 10da ffff ffff description: subtract w and the carry flag (borrow) from register ?f? (2?s comple- ment method). if ?d? is ? 0 ?, the result is stored in w. if ?d? is ? 1 ?, the result is stored back in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example 1 : subwfb reg, 1, 0 before instruction reg = 19h (0001 1001) w =0dh (0000 1101) c=1 after instruction reg = 0ch (0000 1011) w =0dh (0000 1101) c=1 z=0 n = 0 ; result is positive example 2 : subwfb reg, 0, 0 before instruction reg = 1bh (0001 1011) w =1ah (0001 1010) c=0 after instruction reg = 1bh (0001 1011) w = 00h c=1 z = 1 ; result is zero n=0 example 3: subwfb reg, 1, 0 before instruction reg = 03h (0000 0011) w =0eh (0000 1101) c=1 after instruction reg = f5h (1111 0100) ; [2?s comp] w =0eh (0000 1101) c=0 z=0 n = 1 ; result is negative swapf swap f syntax: swapf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f<3:0>) ? dest<7:4>, (f<7:4>) ? dest<3:0> status affected: none encoding: 0011 10da ffff ffff description: the upper and lower nibbles of register ?f? are exchanged. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is placed in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : swapf reg, 1, 0 before instruction reg = 53h after instruction reg = 35h
? 2010 microchip technology inc. preliminary ds41350e-page 347 pic18f/lf1xk50 tblrd table read syntax: tblrd ( *; *+; *-; +*) operands: none operation: if tblrd *, (prog mem (tblptr)) ? tablat; tblptr ? no change; if tblrd *+, (prog mem (tblptr)) ? tablat; (tblptr) + 1 ? tblptr; if tblrd *-, (prog mem (tblptr)) ? tablat; (tblptr) ? 1 ? tblptr; if tblrd +*, (tblptr) + 1 ? tblptr; (prog mem (tblptr)) ? tablat; status affected: none encoding: 0000 0000 0000 10nn nn=0 * =1 *+ =2 *- =3 +* description: this instruction is used to read the contents of program memory (p.m.). to address the program memory, a pointer called table pointer (tblptr) is used. the tblptr (a 21-bit pointer) points to each byte in the program memory. tblptr has a 2-mbyte address range. tblptr[0] = 0 : least significant byte of program memory word tblptr[0] = 1 : most significant byte of program memory word the tblrd instruction can modify the value of tblptr as follows: ? no change ? post-increment ? post-decrement ? pre-increment words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode no operation no operation no operation no operation no operation (read program memory) no operation no operation (write tablat) tblrd table read (continued) example1 : tblrd *+ ; before instruction tablat = 55h tblptr = 00a356h memory (00a356h) = 34h after instruction tablat = 34h tblptr = 00a357h example2 : tblrd +* ; before instruction tablat = aah tblptr = 01a357h memory (01a357h) = 12h memory (01a358h) = 34h after instruction tablat = 34h tblptr = 01a358h
pic18f/lf1xk50 ds41350e-page 348 preliminary ? 2010 microchip technology inc. tblwt table write syntax: tblwt ( *; *+; *-; +*) operands: none operation: if tblwt*, (tablat) ? holding register; tblptr ? no change; if tblwt*+, (tablat) ? holding register; (tblptr) + 1 ? tblptr; if tblwt*-, (tablat) ? holding register; (tblptr) ? 1 ? tblptr; if tblwt+*, (tblptr) + 1 ? tblptr; (tablat) ? holding register; status affected: none encoding: 0000 0000 0000 11nn nn=0 * =1 *+ =2 *- =3 +* description: this instruction uses the 3 lsbs of tblptr to determine which of the 8 holding registers the tablat is written to. the holding registers are used to program the contents of program memory (p.m.). (refer to section 4.0 ?flash program memory? for additional details on programming flash memory.) the tblptr (a 21-bit pointer) points to each byte in the program memory. tblptr has a 2-mbyte address range. the lsb of the tblptr selects which byte of the program memory location to access. tblptr[0] = 0 : least significant byte of program memory word tblptr[0] = 1 : most significant byte of program memory word the tblwt instruction can modify the value of tblptr as follows: ? no change ? post-increment ? post-decrement ? pre-increment words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode no operation no operation no operation no operation no operation (read tablat) no operation no operation (write to holding register ) tblwt table write (continued) example1 : tblwt *+; before instruction tablat = 55h tblptr = 00a356h holding register (00a356h) = ffh after instructions (table write completion) tablat = 55h tblptr = 00a357h holding register (00a356h) = 55h example 2 : tblwt +*; before instruction tablat = 34h tblptr = 01389ah holding register (01389ah) = ffh holding register (01389bh) = ffh after instruction (table write completion) tablat = 34h tblptr = 01389bh holding register (01389ah) = ffh holding register (01389bh) = 34h
? 2010 microchip technology inc. preliminary ds41350e-page 349 pic18f/lf1xk50 tstfsz test f, skip if 0 syntax: tstfsz f {,a} operands: 0 ? f ? 255 a ? [0,1] operation: skip if f = 0 status affected: none encoding: 0110 011a ffff ffff description: if ?f? = 0 , the next instruction fetched during the current instruction execution is discarded and a nop is executed, making this a two-cycle instruction. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here tstfsz cnt, 1 nzero : zero : before instruction pc = address (here) after instruction if cnt = 00h, pc = address (zero) if cnt ? 00h, pc = address (nzero) xorlw exclusive or literal with w syntax: xorlw k operands: 0 ?? k ?? 255 operation: (w) .xor. k ?? w status affected: n, z encoding: 0000 1010 kkkk kkkk description: the contents of w are xored with the 8-bit literal ?k?. the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to w example : xorlw 0afh before instruction w=b5h after instruction w=1ah
pic18f/lf1xk50 ds41350e-page 350 preliminary ? 2010 microchip technology inc. xorwf exclusive or w with f syntax: xorwf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (w) .xor. (f) ?? dest status affected: n, z encoding: 0001 10da ffff ffff description: exclusive or the contents of w with register ?f?. if ?d? is ? 0 ?, the result is stored in w. if ?d? is ? 1 ?, the result is stored back in the register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank (default). if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : xorwf reg, 1, 0 before instruction reg = afh w=b5h after instruction reg = 1ah w=b5h
? 2010 microchip technology inc. preliminary ds41350e-page 351 pic18f/lf1xk50 25.2 extended instruction set in addition to the standard 75 instructions of the pic18 instruction set, pic18f/lf1xk50 devices also provide an optional extension to the core cpu functionality. the added features include eight additional instructions that augment indirect and indexed addressing operations and the implementation of indexed literal offset addressing mode for many of the standard pic18 instructions. the additional features of the extended instruction set are disabled by default. to enable them, users must set the xinst configuration bit. the instructions in the extended set can all be classified as literal operations, which either manipulate the file select registers, or use them for indexed addressing. two of the instructions, addfsr and subfsr , each have an additional special instantiation for using fsr2. these versions ( addulnk and subulnk ) allow for automatic return after execution. the extended instructions are specifically implemented to optimize re-entrant program code (that is, code that is recursive or that uses a software stack) written in high-level languages, particularly c. among other things, they allow users working in high-level languages to perform certain operations on data structures more efficiently. these include: ? dynamic allocation and deallocation of software stack space when entering and leaving subroutines ? function pointer invocation ? software stack pointer manipulation ? manipulation of variables located in a software stack a summary of the instructions in the extended instruc- tion set is provided in tab l e 2 5- 3 . detailed descriptions are provided in section 25.2.2 ?extended instruction set? . the opcode field descriptions in tab l e 2 5- 1 (page 310) apply to both the standard and extended pic18 instruction sets. 25.2.1 extended instruction syntax most of the extended instructions use indexed arguments, using one of the file select registers and some offset to specify a source or destination register. when an argument for an instruction serves as part of indexed addressing, it is enclosed in square brackets (?[ ]?). this is done to indicate that the argument is used as an index or offset. mpasm? assembler will flag an error if it determines that an index or offset value is not bracketed. when the extended instruction set is enabled, brackets are also used to indicate index arguments in byte- oriented and bit-oriented instructions. this is in addition to other changes in their syntax. for more details, see section 25.2.3.1 ?extended instruction syntax with standard pic18 commands? . table 25-3: extensions to the pic18 instruction set note: the instruction set extension and the indexed literal offset addressing mode were designed for optimizing applications written in c; the user may likely never use these instructions directly in assembler. the syntax for these commands is pro- vided as a reference for users who may be reviewing code that has been generated by a compiler. note: in the past, square brackets have been used to denote optional arguments in the pic18 and earlier instruction sets. in this text and going forward, optional arguments are denoted by braces (?{ }?). mnemonic, operands description cycles 16-bit instruction word status affected msb lsb addfsr addulnk callw movsf movss pushl subfsr subulnk f, k k z s , f d z s , z d k f, k k add literal to fsr add literal to fsr2 and return call subroutine using wreg move z s (source) to 1st word f d (destination) 2nd word move z s (source) to 1st word z d (destination) 2nd word store literal at fsr2, decrement fsr2 subtract literal from fsr subtract literal from fsr2 and return 1 2 2 2 2 1 1 2 1110 1110 0000 1110 1111 1110 1111 1110 1110 1110 1000 1000 0000 1011 ffff 1011 xxxx 1010 1001 1001 ffkk 11kk 0001 0zzz ffff 1zzz xzzz kkkk ffkk 11kk kkkk kkkk 0100 zzzz ffff zzzz zzzz kkkk kkkk kkkk none none none none none none none none
pic18f/lf1xk50 ds41350e-page 352 preliminary ? 2010 microchip technology inc. 25.2.2 extended instruction set addfsr add literal to fsr syntax: addfsr f, k operands: 0 ? k ? 63 f ? [ 0, 1, 2 ] operation: fsr(f) + k ? fsr(f) status affected: none encoding: 1110 1000 ffkk kkkk description: the 6-bit literal ?k? is added to the contents of the fsr specified by ?f?. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to fsr example: addfsr 2, 23h before instruction fsr2 = 03ffh after instruction fsr2 = 0422h addulnk add literal to fsr2 and return syntax: addulnk k operands: 0 ? k ? 63 operation: fsr2 + k ? fsr2, (tos) ?? pc status affected: none encoding: 1110 1000 11kk kkkk description: the 6-bit literal ?k? is added to the contents of fsr2. a return is then executed by loading the pc with the tos. the instruction takes two cycles to execute; a nop is performed during the second cycle. this may be thought of as a special case of the addfsr instruction, where f = 3 (binary ? 11 ?); it operates only on fsr2. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to fsr no operation no operation no operation no operation example: addulnk 23h before instruction fsr2 = 03ffh pc = 0100h after instruction fsr2 = 0422h pc = (tos) note: all pic18 instructions may take an optional label argument preceding the instruction mnemonic for use in symbolic addressing. if a label is used, the instruction syntax then becomes: {label} instruction argument(s).
? 2010 microchip technology inc. preliminary ds41350e-page 353 pic18f/lf1xk50 callw subroutine call using wreg syntax: callw operands: none operation: (pc + 2) ? tos, (w) ? pcl, (pclath) ? pch, (pclatu) ? pcu status affected: none encoding: 0000 0000 0001 0100 description first, the return address (pc + 2) is pushed onto the return stack. next, the contents of w are written to pcl; the existing value is discarded. then, the contents of pclath and pclatu are latched into pch and pcu, respectively. the second cycle is executed as a nop instruction while the new next instruction is fetched. unlike call , there is no option to update w, status or bsr. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read wreg push pc to stack no operation no operation no operation no operation no operation example : here callw before instruction pc = address (here) pclath = 10h pclatu = 00h w = 06h after instruction pc = 001006h tos = address (here + 2) pclath = 10h pclatu = 00h w = 06h movsf move indexed to f syntax: movsf [z s ], f d operands: 0 ? z s ? 127 0 ? f d ? 4095 operation: ((fsr2) + z s ) ? f d status affected: none encoding: 1st word (source) 2nd word (destin.) 1110 1111 1011 ffff 0zzz ffff zzzz s ffff d description: the contents of the source register are moved to destination register ?f d ?. the actual address of the source register is determined by adding the 7-bit literal offset ?z s ? in the first word to the value of fsr2. the address of the destination register is specified by the 12-bit literal ?f d ? in the second word. both addresses can be anywhere in the 4096-byte data space (000h to fffh). the movsf instruction cannot use the pcl, tosu, tosh or tosl as the destination register. if the resultant source address points to an indirect addressing register, the value returned will be 00h. words: 2 cycles: 2 q cycle activity: q1 q2 q3 q4 decode determine source addr determine source addr read source reg decode no operation no dummy read no operation write register ?f? (dest) example : movsf [05h], reg2 before instruction fsr2 = 80h contents of 85h = 33h reg2 = 11h after instruction fsr2 = 80h contents of 85h = 33h reg2 = 33h
pic18f/lf1xk50 ds41350e-page 354 preliminary ? 2010 microchip technology inc. movss move indexed to indexed syntax: movss [z s ], [z d ] operands: 0 ? z s ? 127 0 ? z d ? 127 operation: ((fsr2) + z s ) ? ((fsr2) + z d ) status affected: none encoding: 1st word (source) 2nd word (dest.) 1110 1111 1011 xxxx 1zzz xzzz zzzz s zzzz d description the contents of the source register are moved to the destination register. the addresses of the source and destination registers are determined by adding the 7-bit literal offsets ?z s ? or ?z d ?, respectively, to the value of fsr2. both registers can be located anywhere in the 4096-byte data memory space (000h to fffh). the movss instruction cannot use the pcl, tosu, tosh or tosl as the destination register. if the resultant source address points to an indirect addressing register, the value returned will be 00h. if the resultant destination address points to an indirect addressing register, the instruction will execute as a nop . words: 2 cycles: 2 q cycle activity: q1 q2 q3 q4 decode determine source addr determine source addr read source reg decode determine dest addr determine dest addr write to dest reg example : movss [05h], [06h] before instruction fsr2 = 80h contents of 85h = 33h contents of 86h = 11h after instruction fsr2 = 80h contents of 85h = 33h contents of 86h = 33h pushl store literal at fsr2, decrement fsr2 syntax: pushl k operands: 0 ??? k ? 255 operation: k ? (fsr2), fsr2 ? 1 ? fsr2 status affected: none encoding: 1111 1010 kkkk kkkk description: the 8-bit literal ?k? is written to the data memory address specified by fsr2. fsr2 is decremented by 1 after the operation. this instruction allows users to push values onto a software stack. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read ?k? process data write to destination example : pushl 08h before instruction fsr2h:fsr2l = 01ech memory (01ech) = 00h after instruction fsr2h:fsr2l = 01ebh memory (01ech) = 08h
? 2010 microchip technology inc. preliminary ds41350e-page 355 pic18f/lf1xk50 subfsr subtract literal from fsr syntax: subfsr f, k operands: 0 ? k ? 63 f ? [ 0, 1, 2 ] operation: fsr(f) ? k ? fsrf status affected: none encoding: 1110 1001 ffkk kkkk description: the 6-bit literal ?k? is subtracted from the contents of the fsr specified by ?f?. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : subfsr 2, 23h before instruction fsr2 = 03ffh after instruction fsr2 = 03dch subulnk subtract literal from fsr2 and return syntax: subulnk k operands: 0 ? k ? 63 operation: fsr2 ? k ? fsr2 (tos) ?? pc status affected: none encoding: 1110 1001 11kk kkkk description: the 6-bit literal ?k? is subtracted from the contents of the fsr2. a return is then executed by loading the pc with the tos. the instruction takes two cycles to execute; a nop is performed during the second cycle. this may be thought of as a special case of the subfsr instruction, where f = 3 (binary ? 11 ?); it operates only on fsr2. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination no operation no operation no operation no operation example : subulnk 23h before instruction fsr2 = 03ffh pc = 0100h after instruction fsr2 = 03dch pc = (tos)
pic18f/lf1xk50 ds41350e-page 356 preliminary ? 2010 microchip technology inc. 25.2.3 byte-oriented and bit-oriented instructions in indexed literal offset mode in addition to eight new commands in the extended set, enabling the extended instruction set also enables indexed literal offset addressing mode ( section 3.5.1 ?indexed addressing with literal offset? ). this has a significant impact on the way that many commands of the standard pic18 instruction set are interpreted. when the extended set is disabled, addresses embedded in opcodes are treated as literal memory locations: either as a location in the access bank (?a? = 0 ), or in a gpr bank designated by the bsr (?a? = 1 ). when the extended instruction set is enabled and ?a? = 0 , however, a file register argument of 5fh or less is interpreted as an offset from the pointer value in fsr2 and not as a literal address. for practical purposes, this means that all instructions that use the access ram bit as an argument ? that is, all byte-oriented and bit- oriented instructions, or almost half of the core pic18 instructions ? may behave differently when the extended instruction set is enabled. when the content of fsr2 is 00h, the boundaries of the access ram are essentially remapped to their original values. this may be useful in creating backward compatible code. if this technique is used, it may be necessary to save the value of fsr2 and restore it when moving back and forth between c and assembly routines in order to preserve the stack pointer. users must also keep in mind the syntax requirements of the extended instruction set (see section 25.2.3.1 ?extended instruction syntax with standard pic18 commands? ). although the indexed literal offset addressing mode can be very useful for dynamic stack and pointer manipulation, it can also be very annoying if a simple arithmetic operation is carried out on the wrong register. users who are accustomed to the pic18 programming must keep in mind that, when the extended instruction set is enabled, register addresses of 5fh or less are used for indexed literal offset addressing. representative examples of typical byte-oriented and bit-oriented instructions in the indexed literal offset addressing mode are provided on the following page to show how execution is affected. the operand condi- tions shown in the examples are applicable to all instructions of these types. 25.2.3.1 extended instruction syntax with standard pic18 commands when the extended instruction set is enabled, the file register argument, ?f?, in the standard byte-oriented and bit-oriented commands is replaced with the literal offset value, ?k?. as already noted, this occurs only when ?f? is less than or equal to 5fh. when an offset value is used, it must be indicated by square brackets (?[ ]?). as with the extended instructions, the use of brackets indicates to the compiler that the value is to be interpreted as an index or an offset. omitting the brackets, or using a value greater than 5fh within brackets, will generate an error in the mpasm? assembler. if the index argument is properly bracketed for indexed literal offset addressing, the access ram argument is never specified; it will automatically be assumed to be ? 0 ?. this is in contrast to standard operation (extended instruction set disabled) when ?a? is set on the basis of the target address. declaring the access ram bit in this mode will also generate an error in the mpasm assembler. the destination argument, ?d?, functions as before. in the latest versions of the mpasm assembler, language support for the extended instruction set must be explicitly invoked. this is done with either the command line option, /y , or the pe directive in the source listing. 25.2.4 considerations when enabling the extended instruction set it is important to note that the extensions to the instruc- tion set may not be beneficial to all users. in particular, users who are not writing code that uses a software stack may not benefit from using the extensions to the instruction set. additionally, the indexed literal offset addressing mode may create issues with legacy applications written to the pic18 assembler. this is because instructions in the legacy code may attempt to address registers in the access bank below 5fh. since these addresses are interpreted as literal offsets to fsr2 when the instruction set extension is enabled, the application may read or write to the wrong data addresses. when porting an application to the pic18f/lf1xk50, it is very important to consider the type of code. a large, re-entrant application that is written in ?c? and would benefit from efficient compilation will do well when using the instruction set extensions. legacy applica- tions that heavily use the access bank will most likely not benefit from using the extended instruction set. note: enabling the pic18 instruction set extension may cause legacy applications to behave erratically or fail entirely.
? 2010 microchip technology inc. preliminary ds41350e-page 357 pic18f/lf1xk50 addwf add w to indexed (indexed literal offset mode) syntax: addwf [k] {,d} operands: 0 ? k ? 95 d ? [0,1] operation: (w) + ((fsr2) + k) ? dest status affected: n, ov, c, dc, z encoding: 0010 01d0 kkkk kkkk description: the contents of w are added to the contents of the register indicated by fsr2, offset by the value ?k?. if ?d? is ? 0 ?, the result is stored in w. if ?d? is ? 1 ?, the result is stored back in register ?f? (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read ?k? process data write to destination example : addwf [ofst] , 0 before instruction w = 17h ofst = 2ch fsr2 = 0a00h contents of 0a2ch = 20h after instruction w = 37h contents of 0a2ch = 20h bsf bit set indexed (indexed literal offset mode) syntax: bsf [k], b operands: 0 ? f ? 95 0 ? b ? 7 operation: 1 ? ((fsr2) + k) status affected: none encoding: 1000 bbb0 kkkk kkkk description: bit ?b? of the register indicated by fsr2, offset by the value ?k?, is set. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : bsf [flag_ofst], 7 before instruction flag_ofst = 0ah fsr2 = 0a00h contents of 0a0ah = 55h after instruction contents of 0a0ah = d5h setf set indexed (indexed literal offset mode) syntax: setf [k] operands: 0 ? k ? 95 operation: ffh ? ((fsr2) + k) status affected: none encoding: 0110 1000 kkkk kkkk description: the contents of the register indicated by fsr2, offset by ?k?, are set to ffh. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read ?k? process data write register example : setf [ofst] before instruction ofst = 2ch fsr2 = 0a00h contents of 0a2ch = 00h after instruction contents of 0a2ch = ffh
pic18f/lf1xk50 ds41350e-page 358 preliminary ? 2010 microchip technology inc. 25.2.5 special considerations with microchip mplab ? ide tools the latest versions of microchip?s software tools have been designed to fully support the extended instruction set of the pic18f/lf1xk50 family of devices. this includes the mplab ? c18 c compiler, mpasm assembly language and mplab integrated development environment (ide). when selecting a target device for software development, mplab ide will automatically set default configuration bits for that device. the default setting for the xinst configuration bit is ? 0 ?, disabling the extended instruction set and indexed literal offset addressing mode. for proper execution of applications developed to take advantage of the extended instruction set, xinst must be set during programming. to develop software for the extended instruction set, the user must enable support for the instructions and the indexed addressing mode in their language tool(s). depending on the environment being used, this may be done in several ways: ? a menu option, or dialog box within the environment, that allows the user to configure the language tool and its settings for the project ? a command line option ? a directive in the source code these options vary between different compilers, assemblers and development environments. users are encouraged to review the documentation accompanying their development systems for the appropriate information.
? 2010 microchip technology inc. preliminary ds41350e-page 359 pic18f1xk50/pic18lf1xk50 26.0 development support the pic ? microcontrollers and dspic ? digital signal controllers are supported with a full range of software and hardware development tools: ? integrated development environment - mplab ? ide software ? compilers/assemblers/linkers - mplab c compiler for various device families - hi-tech c for various device families - mpasm tm assembler -mplink tm object linker/ mplib tm object librarian - mplab assembler/linker/librarian for various device families ? simulators - mplab sim software simulator ?emulators - mplab real ice? in-circuit emulator ? in-circuit debuggers - mplab icd 3 - pickit? 3 debug express ? device programmers - pickit? 2 programmer - mplab pm3 device programmer ? low-cost demonstration/development boards, evaluation kits, and starter kits 26.1 mplab integrated development environment software the mplab ide software brings an ease of software development previously unseen in the 8/16/32-bit microcontroller market. the mplab ide is a windows ? operating system-based application that contains: ? a single graphical interface to all debugging tools - simulator - programmer (sold separately) - in-circuit emulator (sold separately) - in-circuit debugger (sold separately) ? a full-featured editor with color-coded context ? a multiple project manager ? customizable data windows with direct edit of contents ? high-level source code debugging ? mouse over variable inspection ? drag and drop variables from source to watch windows ? extensive on-line help ? integration of select third party tools, such as iar c compilers the mplab ide allows you to: ? edit your source files (either c or assembly) ? one-touch compile or assemble, and download to emulator and simulator tools (automatically updates all project information) ? debug using: - source files (c or assembly) - mixed c and assembly - machine code mplab ide supports multiple debugging tools in a single development paradigm, from the cost-effective simulators, through low-cost in-circuit debuggers, to full-featured emulators. this eliminates the learning curve when upgrading to tools with increased flexibility and power.
pic18f1xk50/pic18lf1xk50 ds41350e-page 360 preliminary ? 2010 microchip technology inc. 26.2 mplab c compilers for various device families the mplab c compiler code development systems are complete ansi c compilers for microchip?s pic18, pic24 and pic32 families of microcontrollers and the dspic30 and dspic33 families of digital signal control- lers. these compilers provide powerful integration capabilities, superior code optimization and ease of use. for easy source level debugging, the compilers provide symbol information that is optimized to the mplab ide debugger. 26.3 hi-tech c for various device families the hi-tech c compiler code development systems are complete ansi c compilers for microchip?s pic family of microcontrollers and the dspic family of digital signal controllers. these compilers provide powerful integration capabilities, omniscient code generation and ease of use. for easy source level debugging, the compilers provide symbol information that is optimized to the mplab ide debugger. the compilers include a macro assembler, linker, pre- processor, and one-step driver, and can run on multiple platforms. 26.4 mpasm assembler the mpasm assembler is a full-featured, universal macro assembler for pic10/12/16/18 mcus. the mpasm assembler generates relocatable object files for the mplink object linker, intel ? standard hex files, map files to detail memory usage and symbol reference, absolute lst files that contain source lines and generated machine code and coff files for debugging. the mpasm assembler features include: ? integration into mplab ide projects ? user-defined macros to streamline assembly code ? conditional assembly for multi-purpose source files ? directives that allow complete control over the assembly process 26.5 mplink object linker/ mplib object librarian the mplink object linker combines relocatable objects created by the mpasm assembler and the mplab c18 c compiler. it can link relocatable objects from precompiled libraries, using directives from a linker script. the mplib object librarian manages the creation and modification of library files of precompiled code. when a routine from a library is called from a source file, only the modules that contain that routine will be linked in with the application. this allows large libraries to be used efficiently in many different applications. the object linker/library features include: ? efficient linking of single libraries instead of many smaller files ? enhanced code maintainability by grouping related modules together ? flexible creation of libraries with easy module listing, replacement, deletion and extraction 26.6 mplab assembler, linker and librarian for various device families mplab assembler produces relocatable machine code from symbolic assembly language for pic24, pic32 and dspic devices. mplab c compiler uses the assembler to produce its object file. the assembler generates relocatable object files that can then be archived or linked with other relocatable object files and archives to create an executable file. notable features of the assembler include: ? support for the entire device instruction set ? support for fixed-point and floating-point data ? command line interface ? rich directive set ? flexible macro language ? mplab ide compatibility
? 2010 microchip technology inc. preliminary ds41350e-page 361 pic18f1xk50/pic18lf1xk50 26.7 mplab sim software simulator the mplab sim software simulator allows code development in a pc-hosted environment by simulat- ing the pic mcus and dspic ? dscs on an instruction level. on any given instruction, the data areas can be examined or modified and stimuli can be applied from a comprehensive stimulus controller. registers can be logged to files for further run-time analysis. the trace buffer and logic analyzer display extend the power of the simulator to record and track program execution, actions on i/o, most peripherals and internal registers. the mplab sim software simulator fully supports symbolic debugging using the mplab c compilers, and the mpasm and mplab assemblers. the soft- ware simulator offers the flexibility to develop and debug code outside of the hardware laboratory envi- ronment, making it an excellent, economical software development tool. 26.8 mplab real ice in-circuit emulator system mplab real ice in-circuit emulator system is microchip?s next generation high-speed emulator for microchip flash dsc and mcu devices. it debugs and programs pic ? flash mcus and dspic ? flash dscs with the easy-to-use, powerful graphical user interface of the mplab integrated development environment (ide), included with each kit. the emulator is connected to the design engineer?s pc using a high-speed usb 2.0 interface and is connected to the target with either a connector compatible with in- circuit debugger systems (rj11) or with the new high- speed, noise tolerant, low-voltage differential signal (lvds) interconnection (cat5). the emulator is field upgradable through future firmware downloads in mplab ide. in upcoming releases of mplab ide, new devices will be supported, and new features will be added. mplab real ice offers significant advantages over competitive emulators including low-cost, full-speed emulation, run-time variable watches, trace analysis, complex breakpoints, a ruggedized probe interface and long (up to three meters) interconnection cables. 26.9 mplab icd 3 in-circuit debugger system mplab icd 3 in-circuit debugger system is micro- chip's most cost effective high-speed hardware debugger/programmer for microchip flash digital sig- nal controller (dsc) and microcontroller (mcu) devices. it debugs and programs pic ? flash microcon- trollers and dspic ? dscs with the powerful, yet easy- to-use graphical user interface of mplab integrated development environment (ide). the mplab icd 3 in-circuit debugger probe is con- nected to the design engineer's pc using a high-speed usb 2.0 interface and is connected to the target with a connector compatible with the mplab icd 2 or mplab real ice systems (rj-11). mplab icd 3 supports all mplab icd 2 headers. 26.10 pickit 3 in-circuit debugger/ programmer and pickit 3 debug express the mplab pickit 3 allows debugging and program- ming of pic ? and dspic ? flash microcontrollers at a most affordable price point using the powerful graphical user interface of the mplab integrated development environment (ide). the mplab pickit 3 is connected to the design engineer's pc using a full speed usb interface and can be connected to the target via an microchip debug (rj-11) connector (compatible with mplab icd 3 and mplab real ice). the connector uses two device i/o pins and the reset line to imple- ment in-circuit debugging and in-circuit serial pro- gramming?. the pickit 3 debug express include the pickit 3, demo board and microcontroller, hookup cables and cdrom with user?s guide, lessons, tutorial, compiler and mplab ide software.
pic18f1xk50/pic18lf1xk50 ds41350e-page 362 preliminary ? 2010 microchip technology inc. 26.11 pickit 2 development programmer/debugger and pickit 2 debug express the pickit? 2 development programmer/debugger is a low-cost development tool with an easy to use inter- face for programming and debugging microchip?s flash families of microcontrollers. the full featured windows ? programming interface supports baseline (pic10f, pic12f5xx, pic16f5xx), midrange (pic12f6xx, pic16f), pic18f, pic24, dspic30, dspic33, and pic32 families of 8-bit, 16-bit, and 32-bit microcontrollers, and many microchip serial eeprom products. with microchip?s powerful mplab integrated development environment (ide) the pickit? 2 enables in-circuit debugging on most pic ? microcon- trollers. in-circuit-debugging runs, halts and single steps the program while the pic microcontroller is embedded in the application. when halted at a break- point, the file registers can be examined and modified. the pickit 2 debug express include the pickit 2, demo board and microcontroller, hookup cables and cdrom with user?s guide, lessons, tutorial, compiler and mplab ide software. 26.12 mplab pm3 device programmer the mplab pm3 device programmer is a universal, ce compliant device programmer with programmable voltage verification at v ddmin and v ddmax for maximum reliability. it features a large lcd display (128 x 64) for menus and error messages and a modu- lar, detachable socket assembly to support various package types. the icsp? cable assembly is included as a standard item. in stand-alone mode, the mplab pm3 device programmer can read, verify and program pic devices without a pc connection. it can also set code protection in this mode. the mplab pm3 connects to the host pc via an rs-232 or usb cable. the mplab pm3 has high-speed communications and optimized algorithms for quick programming of large memory devices and incorporates an mmc card for file storage and data applications. 26.13 demonstration/development boards, evaluation kits, and starter kits a wide variety of demonstration, development and evaluation boards for various pic mcus and dspic dscs allows quick application development on fully func- tional systems. most boards include prototyping areas for adding custom circuitry and provide application firmware and source code for examination and modification. the boards support a variety of features, including leds, temperature sensors, switches, speakers, rs-232 interfaces, lcd displays, potentiometers and additional eeprom memory. the demonstration and development boards can be used in teaching environments, for prototyping custom circuits and for learning about various microcontroller applications. in addition to the picdem? and dspicdem? demon- stration/development board series of circuits, microchip has a line of evaluation kits and demonstration software for analog filter design, k ee l oq ? security ics, can, irda ? , powersmart battery management, seeval ? evaluation system, sigma-delta adc, flow rate sensing, plus many more. also available are starter kits that contain everything needed to experience the specified device. this usually includes a single application and debug capability, all on one board. check the microchip web page ( www.microchip.com ) for the complete list of demonstration, development and evaluation kits.
? 2010 microchip technology inc. preliminary ds41350e-page 363 pic18f/lf1xk50 27.0 electrical specifications absolute maximum ratings (?) ambient temperature under bias................................................................................................. ...... -40c to +125c storage temperature ............................................................................................................ ............ -65c to +150c voltage on v dd with respect to v ss , pic18f1xk50 .......................................................................... -0.3v to +6.0v voltage on v dd with respect to v ss , pic18lf1xk50 ........................................................................ -0.3v to +4.0v voltage on mclr with respect to vss ................................................................................................. -0.3v to +9.0v voltage on v usb pin with respect to v ss ............................................................................................ -0.3v to +4.0v voltage on d+ and d- pins with respect to v ss ...................................................................... -0.3v to (v usb + 0.3v) voltage on all other pins with respect to v ss ........................................................................... -0.3v to (v dd + 0.3v) total power dissipation (1) ............................................................................................................................... 800 mw maximum current out of v ss pin ...................................................................................................................... 95 m a maximum current into v dd pin ......................................................................................................................... 9 5 ma clamp current, i k (v pin < 0 or v pin > v dd ) ??????????????????????????????????????????????????????????????? ??????????????????????????????????????????????????? 20 ma maximum output current sunk by any i/o pin..................................................................................... ............... 25 ma maximum output current sourced by any i/o pin .................................................................................. ............ 25 ma maximum current sunk by all ports .............................................................................................. ..................... 90 ma maximum current sourced by all ports ........................................................................................... .................. 90 ma note 1: power dissipation is calculated as follows: p dis = v dd x {i dd ? ? i oh } + ? {(v dd ? v oh ) x i oh } + ? (v o l x i ol ). 2: vusb must always be ? v dd + 0.3v ? notice: stresses above those listed under ?absolute maximum ratings? may cause permanent damage to the device. this is a stress rating only and functional operation of the device at those or any other conditions above those indicated in the operation listings of this specification is not implied. exposure above maximum rating conditions for extended periods may affect device reliability.
pic18f/lf1xk50 ds41350e-page 364 preliminary ? 2010 microchip technology inc. 27.1 dc characteristics: pic18f/lf1xk50-i/e (industrial, extended) pic18lf1xk50 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +85c for industrial -40c ? t a ? +125c for extended pic18f1xk50 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +85c for industrial -40c ? t a ? +125c for extended param. no. sym characteristic min typ? max units conditions d001 v dd supply voltage pic18lf1xk50 1.8 2.7 ? ? 3.6 3.6 v v f osc < = 20 mhz f osc < = 48 mhz d001 pic18f1xk50 1.8 2.7 ? ? 5.5 5.5 v v f osc < = 20 mhz f osc < = 48 mhz d002* v dr ram data retention voltage (1) pic18lf1xk50 1.5 ? ? v device in sleep mode d002* pic18f1xk50 1.7 ? ? v device in sleep mode v por * power-on reset release voltage ?1.6? v v porr * power-on reset rearm voltage ?0.8? v d004* s vdd v dd rise rate to ensure internal power-on reset signal 0.05 ? ? v/ms * these parameters are characterized but not tested. ? data in ?typ? column is at 3.0v, 25c unless otherwise st ated. these parameters are for design guidance only and are not tested. note 1: this is the limit to which v dd can be lowered in sleep mode without losing ram data.
? 2010 microchip technology inc. preliminary ds41350e-page 365 pic18f/lf1xk50 figure 27-1: por and por rearm with slow rising v dd v dd v por v porr v ss v ss npor t por (3) por rearm note 1: when npor is low, the device is held in reset. 2: t por 1 ? s typical. 3: t vlow 2.7 ? s typical. t vlow (2)
pic18f/lf1xk50 ds41350e-page 366 preliminary ? 2010 microchip technology inc. 27.2 dc characteristics: pic18f/lf1xk50-i/e (industrial, extended) pic18lf1xk50 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +85c for industrial -40c ? t a ? +125c for extended pic18f1xk50 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +85c for industrial -40c ? t a ? +125c for extended param no. device characteristics min typ? max units conditions v dd note supply current (i dd ) (1, 2) d009 ldo regulator ? 30 ? ? a ? ? 5 ? ? a ? lp clock mode and sleep (requires fvr and bor to be disabled) d010 ? 6.0 9 ? a1.8f osc = 32 khz lp oscillator (4) , -40c ? t a ? +85c ?7 12 ? a3.0 d010 ? 6 11 ? a 1.8 f osc = 32 khz lp oscillator (4) , -40c ? t a ? +85c ? 7 17 ? a 3.0 ? 12 20 ? a 5.0 d011* ? 6.0 12 ? a1.8 f osc = 32 khz lp oscillator -40c ? t a ? +125c ?9.0 16 ? a3.0 d011* ? 8.0 15 ? a 1.8 f osc = 32 khz lp oscillator (4) -40c ? t a ? +125c ? 11 25 ? a 3.0 ? 12 35 ? a 5.0 d011* ? 170 220 ? a1.8f osc = 1 mhz xt oscillator ? 280 370 ? a3.0 d011* ? 200 250 ? a 1.8 f osc = 1 mhz xt oscillator ? 310 400 ? a 3.0 ? 380 490 ? a 5.0 d011* ? 75 110 ? a1.8f osc = 1 mhz xt oscillator cpu idle ? 130 190 ? a3.0 d011* ? 90 130 ? a 1.8 f osc = 1 mhz xt oscillator cpu idle ? 140 210 ? a 3.0 ? 160 250 ? a 5.0 * these parameters are characterized but not tested. note 1: the test conditions for all i dd measurements in active operation mode ar e: osc1 = external square wave, from rail-to-rail; all i/o pins tri-stated, pulled to v dd ; mclr = v dd ; wdt disabled. 2: the supply current is mainly a function of the operating voltage and frequency. other factors, such as i/o pin loading and switching rate, oscillator type, internal code execution pa ttern and temperature, also have an impact on the current consumption. 3: for rc oscillator configurations, current through r ext is not included. the current through the resistor can be extended by the formula i r = v dd /2r ext (ma) with r ext in k ?? 4: fvr and bor are disabled. 5: 330 nf capacitor on v usb pin.
? 2010 microchip technology inc. preliminary ds41350e-page 367 pic18f/lf1xk50 supply current (i dd ) (1, 2) d012 ? 300 700 ? a1.8f osc = 4 mhz xt oscillator ? 500 1200 ? a3.0 d012 ? 330 700 ? a 1.8 f osc = 4 mhz xt oscillator ? 530 1200 ? a 3.0 ? 730 1400 ? a 5.0 d012a ? 240 300 ? a1.8f osc = 4 mhz, xt oscillator cpu idle ? 440 550 ? a3.0 d012a ? 230 300 ? a 1.8 f osc = 4 mhz xt oscillator cpu idle ? 400 550 ? a 3.0 ? 470 640 ? a 5.0 d013 ? 140 180 ? a1.8f osc = 1 mhz ec oscillator (medium power) ? 230 300 ? a3.0 d013 ? 160 210 ? a 1.8 f osc = 1 mhz ec oscillator (medium power) (5) ? 250 310 ? a 3.0 ? 290 380 ? a 5.0 d013a ? 50 64 ? a1.8f osc = 1 mhz ec oscillator (medium power) cpu idle ?86110 ? a3.0 d013a ? 70 100 ? a 1.8 f osc = 1 mhz ec oscillator (medium power) cpu idle (5) ? 100 150 ? a 3.0 ? 120 170 ? a 5.0 d014 ? 500 640 ? a1.8f osc = 4 mhz ec oscillator (medium power) ? 830 1100 ? a3.0 d014 ? 520 770 ? a 1.8 f osc = 4 mhz ec oscillator (medium power) (5) ? 860 1200 ? a 3.0 ? 1000 1370 ? a 5.0 27.2 dc characteristics: pic18f/lf1xk50-i/e (industrial, extended) (continued) pic18lf1xk50 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +85c for industrial -40c ? t a ? +125c for extended pic18f1xk50 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +85c for industrial -40c ? t a ? +125c for extended param no. device characteristics min typ? max units conditions v dd note * these parameters are char acterized but not tested. note 1: the test conditions for all i dd measurements in active operation mode are: osc1 = external square wave, from rail-to-rail; all i/o pins tri-stated, pulled to v dd ; mclr = v dd ; wdt disabled. 2: the supply current is mainly a function of the operating voltage and frequency. other factors, such as i/o pin loading and switching rate, oscillator type, internal code execution pa ttern and temperature, also have an impact on the current consumption. 3: for rc oscillator configurations, current through r ext is not included. the current through the resistor can be extended by the formula i r = v dd /2r ext (ma) with r ext in k ?? 4: fvr and bor are disabled. 5: 330 nf capacitor on v usb pin.
pic18f/lf1xk50 ds41350e-page 368 preliminary ? 2010 microchip technology inc. supply current (i dd ) (1, 2) d014a ? 200 250 ? a1.8f osc = 4 mhz ec oscillator (medium power) cpu idle ? 340 440 ? a3.0 d014a ? 210 303 ? a 1.8 f osc = 4 mhz ec oscillator (medium power) cpu idle (5) ? 360 520 ? a 3.0 ? 430 670 ? a 5.0 d015 ? 820 1000 ? a1.8f osc = 6 mhz ec oscillator (high power) ? 1500 1900 ? a3.0 d015 ? 830 1100 ? a 1.8 f osc = 6 mhz ec oscillator (high power) (5) ? 1500 1900 ? a 3.0 ? 1700 2300 ? a 5.0 d015a ? 300 370 ? a1.8f osc = 6 mhz ec oscillator (high power) cpu idle ? 510 660 ? a3.0 d015a ? 320 430 ? a 1.8 f osc = 6 mhz ec oscillator (high power) cpu idle (5) ? 530 690 ? a 3.0 ? 640 840 ? a 5.0 d015b ? 4.7 6.0 ma 3.0 f osc = 24 mhz 6 mhz ec oscillator (high power) pll enabled d015b ? 4.7 6.1 ma 3.0 f osc = 24 mhz 6 mhz ec oscillator (high power) pll enabled (5) ? 5.6 7.4 ma 5.0 d015c ? 2.0 2.5 ma 3.0 f osc = 24 mhz 6 mhz ec oscillator (high power) pll enabled, cpu idle d015c ? 2.0 2.5 ma 3.0 f osc = 24 mhz 6 mhz ec oscillator (high power) pll enabled, cpu idle (5) ? 2.3 3.0 ma 5.0 27.2 dc characteristics: pic18f/lf1xk50-i/e (industrial, extended) (continued) pic18lf1xk50 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +85c for industrial -40c ? t a ? +125c for extended pic18f1xk50 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +85c for industrial -40c ? t a ? +125c for extended param no. device characteristics min typ? max units conditions v dd note * these parameters are characterized but not tested. note 1: the test conditions for all i dd measurements in active operation mode ar e: osc1 = external square wave, from rail-to-rail; all i/o pins tri-stated, pulled to v dd ; mclr = v dd ; wdt disabled. 2: the supply current is mainly a function of the operating voltage and frequency. other factors, such as i/o pin loading and switching rate, oscillator type, internal code execution pa ttern and temperature, also have an impact on the current consumption. 3: for rc oscillator configurations, current through r ext is not included. the current through the resistor can be extended by the formula i r = v dd /2r ext (ma) with r ext in k ?? 4: fvr and bor are disabled. 5: 330 nf capacitor on v usb pin.
? 2010 microchip technology inc. preliminary ds41350e-page 369 pic18f/lf1xk50 27.2 dc characteristics: pic18f/lf1xk50-i/e (industrial, extended) (continued) pic18lf1xk50 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +85c for industrial -40c ? t a ? +125c for extended pic18f1xk50 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +85c for industrial -40c ? t a ? +125c for extended param no. device characteristics min typ? max units conditions v dd note supply current (i dd ) (1, 2) d016 ? 2.6 3.3 ma 3.0 f osc = 12 mhz ec oscillator (high power) d016 ? 2.6 3.3 ma 3.0 f osc = 12 mhz ec oscillator (high power) (5) ? 3.1 4.1 ma 5.0 d017 ? 1.0 1.3 ma 3.0 f osc = 12 mhz ec oscillator (high power) cpu idle d017 ? 1.0 1.3 ma 3.0 f osc = 12 mhz ec oscillator (high power) cpu idle (5) ? 1.2 1.6 ma 5.0 d017a ? 9 12 ma 3.0 f osc = 48 mhz 12 mhz ec oscillator (high power) pll enabled d017a ? 8.9 12 ma 3.0 f osc = 48 mhz 12 mhz ec oscillator (high power) pll enabled (5) ? 11 14 ma 5.0 d017b ? 3.9 5.0 ma 3.0 f osc = 48 mhz 12 mhz ec oscillator (high power) pll enabled, cpu idle d017b ? 3.9 5.0 ma 3.0 f osc = 48 mhz 12 mhz ec oscillator (high power) pll enabled, cpu idle (5) ? 4.7 6.0 ma 5.0 d018 ? 19 38 ? a1.8f osc = 32 khz lfintosc oscillator mode (3, 5) ?23 44 ? a3.0 d018 ? 21 40 ? a 1.8 f osc = 32 khz lfintosc oscillator mode (3, 5) ? 25 46 ? a 3.0 ? 26 48 ? a 5.0 d019 ? 16 33 ? a1.8f osc = 32 khz lfintosc oscillator cpu idle ?18 38 ? a3.0 d019 ? 18 35 ? a 1.8 f osc = 32 khz lfintosc oscillator cpu idle (5) ? 20 40 ? a 3.0 ? 21 42 ? a 5.0 supply current (i dd ) (1, 2) * these parameters are char acterized but not tested. note 1: the test conditions for all i dd measurements in active operation mode are: osc1 = external square wave, from rail-to-rail; all i/o pins tri-stated, pulled to v dd ; mclr = v dd ; wdt disabled. 2: the supply current is mainly a function of the operating voltage and frequency. other factors, such as i/o pin loading and switching rate, oscillator type, internal code execution pa ttern and temperature, also have an impact on the current consumption. 3: for rc oscillator configurations, current through r ext is not included. the current through the resistor can be extended by the formula i r = v dd /2r ext (ma) with r ext in k ?? 4: fvr and bor are disabled. 5: 330 nf capacitor on v usb pin.
pic18f/lf1xk50 ds41350e-page 370 preliminary ? 2010 microchip technology inc. d020 ? 320 470 ? a1.8f osc = 500 khz lfintosc oscillator ? 460 670 ? a3.0 d020 ? 350 500 ? a 1.8 f osc = 500 khz lfintosc oscillator (5) ? 490 700 ? a 3.0 ? 540 780 ? a 5.0 d021 ? 380 600 ? a1.8f osc = 1 mhz hfintosc oscillator ? 550 870 ? a3.0 d021 ? 410 600 ? a 1.8 f osc = 1 mhz hfintosc oscillator (5) ? 580 870 ? a 3.0 ? 650 970 ? a 5.0 d021a ? 290 600 ? a1.8f osc = 1 mhz hfintosc oscillator cpu idle ? 410 760 ? a3.0 d021a ? 320 620 ? a 1.8 f osc = 1 mhz hfintosc oscillator cpu idle (5) ? 440 770 ? a 3.0 ? 490 880 ? a 5.0 d022 ? 1.2 1.6 ma 1.8 f osc = 8 mhz hfintosc oscillator ?2.12.9ma 3.0 d022 ? 1.2 1.6 ma 1.8 f osc = 8 mhz hfintosc oscillator (5) ? 2.1 2.9 ma 3.0 ? 2.4 3.5 ma 5.0 d023 ? 2.0 2.7 ma 1.8 f osc = 16 mhz hfintosc oscillator ?3.54.8ma 3.0 d023 ? 2.0 2.7 ma 1.8 f osc = 16 mhz hfintosc oscillator (5) ? 3.5 4.8 ma 3.0 ? 4.0 6.0 ma 5.0 d023a ? 0.9 1.3 ma 1.8 f osc = 16 mhz hfintosc oscillator cpu idle ?1.52.1ma 3.0 d023a ? 0.9 1.3 ma 1.8 f osc = 16 mhz hfintosc oscillator cpu idle (5) ? 1.5 2.1 ma 3.0 ? 1.7 2.6 ma 5.0 supply current (i dd ) (1, 2) 27.2 dc characteristics: pic18f/lf1xk50-i/e (industrial, extended) (continued) pic18lf1xk50 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +85c for industrial -40c ? t a ? +125c for extended pic18f1xk50 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +85c for industrial -40c ? t a ? +125c for extended param no. device characteristics min typ? max units conditions v dd note * these parameters are characterized but not tested. note 1: the test conditions for all i dd measurements in active operation mode ar e: osc1 = external square wave, from rail-to-rail; all i/o pins tri-stated, pulled to v dd ; mclr = v dd ; wdt disabled. 2: the supply current is mainly a function of the operating voltage and frequency. other factors, such as i/o pin loading and switching rate, oscillator type, internal code execution pa ttern and temperature, also have an impact on the current consumption. 3: for rc oscillator configurations, current through r ext is not included. the current through the resistor can be extended by the formula i r = v dd /2r ext (ma) with r ext in k ?? 4: fvr and bor are disabled. 5: 330 nf capacitor on v usb pin.
? 2010 microchip technology inc. preliminary ds41350e-page 371 pic18f/lf1xk50 d024 ? 0.5 0.7 ma 1.8 f osc = 4 mhz extrc oscillator mode ?0.91.1ma 3.0 d024 ? 0.5 0.7 ma 1.8 f osc = 4 mhz extrc oscillator mode (5) ? 0.9 1.1 ma 3.0 ? 1.0 1.4 ma 5.0 d025 ? 1.0 1.5 ma 1.8 f osc = 6 mhz hs oscillator ?1.72.1ma 3.0 d025 ? 1.0 1.5 ma 1.8 f osc = 6 mhz hs oscillator (5) ? 1.7 2.1 ma 3.0 ? 2.1 2.5 ma 5.0 d025a ? 5.4 6.0 ma 3.0 f osc = 24 mhz 6 mhz hs oscillator pll enabled d025a ? 5.4 6.0 ma 3.0 f osc = 24 mhz 6 mhz hs oscillator pll enabled (5) ? 7.4 7.6 ma 5.0 d026 ? 3.2 3.3 ma 3.0 f osc = 12 mhz hs oscillator d026 ? 3.2 3.3 ma 3.0 f osc = 12 mhz hs oscillator (5) ? 4.8 4.2 ma 5.0 d026a ? 10 12 ma 3.0 f osc = 48 mhz, 12 mhz hs oscillator pll enabled d026a ? 10 12 ma 3.0 f osc = 48 mhz, 12 mhz hs oscillator pll enabled (5) ? 13 15 ma 5.0 27.2 dc characteristics: pic18f/lf1xk50-i/e (industrial, extended) (continued) pic18lf1xk50 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +85c for industrial -40c ? t a ? +125c for extended pic18f1xk50 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +85c for industrial -40c ? t a ? +125c for extended param no. device characteristics min typ? max units conditions v dd note * these parameters are char acterized but not tested. note 1: the test conditions for all i dd measurements in active operation mode are: osc1 = external square wave, from rail-to-rail; all i/o pins tri-stated, pulled to v dd ; mclr = v dd ; wdt disabled. 2: the supply current is mainly a function of the operating voltage and frequency. other factors, such as i/o pin loading and switching rate, oscillator type, internal code execution pa ttern and temperature, also have an impact on the current consumption. 3: for rc oscillator configurations, current through r ext is not included. the current through the resistor can be extended by the formula i r = v dd /2r ext (ma) with r ext in k ?? 4: fvr and bor are disabled. 5: 330 nf capacitor on v usb pin.
pic18f/lf1xk50 ds41350e-page 372 preliminary ? 2010 microchip technology inc. 27.3 dc characteristics: pic18f/lf1xk50-i/e (power-down) pic18lf1xk50 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +85c for industrial -40c ? t a ? +125c for extended pic18f1xk50 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +85c for industrial -40c ? t a ? +125c for extended param no. device characteristics min typ? max +85c max +125c units conditions v dd note power-down base current (i pd ) (2) d027 ? 0.024 0.7 6.7 ? a 1.8 wdt, bor, fvr, voltage regulator and t1osc disabled, all peripherals inactive ? 0.078 1.9 8.5 ? a3.0 d027 ? 6.0 7.0 13 ? a 1.8 wdt, bor, fvr and t1osc disabled, all peripherals inactive ? 7.0 10 15 ? a 3.0 ? 8.0 12 19 ? a 5.0 power-down module current d028 ? 0.45 1.3 4.4 ? a 1.8 lpwdt current (1) ? 0.75 2.0 6.0 ? a3.0 d028 ? 6.5 7.0 10.5 ? a 1.8 lpwdt current (1) ? 9.6 10.6 17.6 ? a 3.0 ? 10.5 16.5 20 ? a 5.0 d029 ? 12 17 23 ? a 1.8 fvr current (3) ?22 19 25 ? a3.0 d029 ? 28 42 50 ? a 1.8 fvr current (3, 5) ? 35.6 45.6 55 ? a 3.0 ? 38.5 49 60 ? a 5.0 d030 ? ? 21 27 ? a 3.0 bor current (1, 3) d030 ? 27 48 51 ? a 3.0 bor current (1, 3, 5) ? 36.5 51 55 ? a 5.0 d031 ? 0.79 3.6 5.3 ? a 1.8 t1osc current (1) ? 1.8 2.9 6.9 ? a3.0 d031 ? 8.0 7.5 10 ? a 1.8 t1osc current (1) ? 8.5 10.5 15 ? a 3.0 ? 10.5 12.5 24 ? a 5.0 * these parameters are characterized but not tested. ? data in ?typ? column is at 3.0v, 25c unless otherwise stated. these parameters are for design guidance only and are not tested. note 1: the peripheral current is the sum of the base i dd or i pd and the additional current consumed when this peripheral is enabled. the peripheral ? current can be determined by subtracting the base i dd or i pd current from this limit. max values should be used when calculating total current consumption. 2: the power-down current in sleep mode does not depend on the oscillator type. power-down current is measured with the part in sleep mode, with all i/o pins in high-impedance state and tied to v dd . 3: fixed voltage reference is automatic ally enabled whenever the bor is enabled 4: a/d oscillator source is f rc 5: 330 ? f capacitor on vusb pin.
? 2010 microchip technology inc. preliminary ds41350e-page 373 pic18f/lf1xk50 power-down module current d032 ? ? 1.8 8 ? a 1.8 a/d current (1, 4) , no conversion in progress ?? 3 10 ? a3.0 d032 ? ? 6 12 ? a 1.8 a/d current (1, 4) , no conversion in progress ? ? 10 17 ? a 3.0 ? ? 11.5 22 ? a 5.0 d033 ? ? 38 44 ? a 1.8 comparator current, low power ? ? 40 47 ? a3.0 d033 ? 30 40 49 ? a 2.0 comparator current, low power ? 34 44 53 ? a 3.0 ? 36 50 60 ? a 5.0 d033a ? ? 239 244 ? a 1.8 comparator current, high power ? ? 242 249 ? a3.0 d033a ? 144 243 250 ? a 2.0 comparator current, high power ? 146 247 256 ? a 3.0 ? 151 253 264 ? a 5.0 d034 ? ? 18 23 ? a 1.8 voltage reference current ? ? 30 35 ? a3.0 d034 ? 35 36 44 ? a 2.0 voltage reference current ? 43 44 60 ? a 3.0 ? 55 65 74 ? a 5.0 27.3 dc characteristics: pic18f/lf1xk50-i/e (power-down) (continued) pic18lf1xk50 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +85c for industrial -40c ? t a ? +125c for extended pic18f1xk50 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +85c for industrial -40c ? t a ? +125c for extended param no. device characteristics min typ? max +85c max +125c units conditions v dd note * these parameters are char acterized but not tested. ? data in ?typ? column is at 3.0v, 25c unless otherwise stated. these parameters are for design guidance only and are not tested. note 1: the peripheral current is the sum of the base i dd or i pd and the additional current consumed when this peripheral is enabled. the peripheral ? current can be determined by subtracting the base i dd or i pd current from this limit. max values should be used when calculating total current consumption. 2: the power-down current in sleep mode does not depend on the oscillator type. power-down current is measured with the part in sleep mode, with all i/o pins in high-impedance state and tied to v dd . 3: fixed voltage reference is automatic ally enabled whenever the bor is enabled 4: a/d oscillator source is f rc 5: 330 ? f capacitor on vusb pin.
pic18f/lf1xk50 ds41350e-page 374 preliminary ? 2010 microchip technology inc. 27.4 dc characteristics: pic18f/lf1xk50-i/e dc characteristics standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +85c for industrial -40c ? t a ? +125c for extended param no. sym characteristic min typ? max units conditions v il input low voltage i/o port: d036 with ttl buffer ? ? 0.8 v 4.5v ? v dd ? 5.5v d036a ? ? 0.15 v dd v1.8v ? v dd ? 4.5v d037 with schmitt trigger buffer ? ? 0.2 v dd v1.8v ? v dd ? 5.5v with i 2 c levels ? ? 0.3 v dd v d038 mclr , osc1 (rc mode) (1) ??0.2v dd v d039a osc1 (hs mode) ? ? 0.3 v dd v v ih input high voltage i/o ports: ? ? d040 with ttl buffer 2.0 ? ? v 4.5v ? v dd ?? 5.5v d040a 0.25 v dd + 0.8 ??v1.8v ? v dd ? 4.5v d041 with schmitt trigger buffer 0.8 v dd ??v1.8v ? v dd ? 5.5v with i 2 c levels 0.7 v dd ??v d042 mclr 0.8 v dd ??v d043a osc1 (hs mode) 0.7 v dd ??v d043b osc1 (rc mode) 0.9 v dd ??v (note 1) i il input leakage current (2) d060 i/o ports ? 5 100 na v ss ? v pin ? v dd , pin at high-impedance d061 mclr (3) ? 50 200nav ss ? v pin ? v dd i pur portb weak pull-up current d070* 50 250 400 ? av dd = 5.0v, v pin = v ss v ol output low voltage (4) d080 i/o ports ?? v ss +0.6 v ss +0.6 v ss +0.6 v i oh = 8ma, v dd = 5v i oh = 6ma, v dd = 3.3v i oh = 1.8ma, v dd = 1.8v v oh output high voltage (4) d090 i/o ports v dd -0.7 v dd -0.7 v dd -0.7 ??v i ol = 3.5ma, v dd = 5v i ol = 3ma, v dd = 3.3v i ol = 1ma, v dd = 1.8v * these parameters are characterized but not tested. ? data in ?typ? column is at 3.0v, 25c unless otherwise stated. these parameters are for design guidance only and are not tested. note 1: in rc oscillator configuration, the osc1/clkin pin is a schm itt trigger input. it is not recommended to use an external clock in rc mode. 2: negative current is defined as current sourced by the pin. 3: the leakage current on the mclr pin is strongly dependent on the applied voltage level. the specified levels represent normal operating conditions. higher leakage current may be measured at different input voltages. 4: including osc2 in clkout mode.
? 2010 microchip technology inc. preliminary ds41350e-page 375 pic18f/lf1xk50 capacitive loading specs on output pins d101* cosc2 osc2 pin ? ? 15 pf in xt, hs and lp modes when external clock is used to drive osc1 d101a* c io all i/o pins ? ? 50 pf flash memory d130 e p cell endurance 10k 100k ? ? e/w program flash memory data flash memory d131 v dd for read v min ??v voltage on mclr /v pp during erase/program 8.0 ? 9.0 v temperature during programming: -40c ? t a ? 85c v dd for bulk erase 2.7 ? v dd max v temperature during programming: 10c ? t a ? 40c d132 v pew v dd for write or row erase v dd min ? v dd max v v min = minimum operating voltage v max = maximum operating voltage i pppgm current on mclr /v pp during erase/write ??1.0ma i ddpgm current on v dd during erase/write ? 5.0 ma d133 t pew erase/write cycle time ? 4.0 5.0 ms d134 t retd characteristic retention 40 ? ? year pr ovided no other specifications are violated vusb capacitor charging d135 charging current ? 200 ? ? a d135a source/sink capability when charging complete ?0.0?ma 27.4 dc characteristics: pic18f/lf1xk50-i/e (continued) dc characteristics standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +85c for industrial -40c ? t a ? +125c for extended param no. sym characteristic min typ? max units conditions * these parameters are char acterized but not tested. ? data in ?typ? column is at 3.0v, 25c unless otherwise stated. these parameters are for design guidance only and are not tested. note 1: in rc oscillator configuration, the osc1/clkin pin is a schm itt trigger input. it is not recommended to use an external clock in rc mode. 2: negative current is defined as current sourced by the pin. 3: the leakage current on the mclr pin is strongly dependent on the applied voltage level. the specified levels represent normal operating conditions. higher leakage current may be measured at different input voltages. 4: including osc2 in clkout mode.
pic18f/lf1xk50 ds41350e-page 376 preliminary ? 2010 microchip technology inc. 27.5 usb module specifications operating conditions-40c ? t a ? +85c (unless otherwise state) param no. sym characteristic min typ max units conditions d313 v usb usb voltage 3.0 ? 3.6 v voltage on v usb pin must be in this range for proper usb operation d314 i il input leakage on pin ? ? 1 ? av ss ?? v pin ?? v dd pin at ? high ? impedance d315 v ilusb input low voltage for usb buffer ??0.8vfor v usb range d316 v ihusb input high voltage for usb buffer 2.0 ? ? v for v usb range d318 v difs differential input sensitivity ? ? 0.2 v the difference between d+ and d- must exceed this value while v cm is met d319 v cm differential common mode range 0.8 ? 2.5 v d320 z out driver output impedance (1) 28 ? 44 ? d321 v ol voltage output low 0.0 ? 0.3 v 1.5 k ?? load connected to 3.6v d322 v oh voltage output high 2.8 ? 3.6 v 1.5 k ?? load connected to ground note 1: the d+ and d- signal lines have been built-in impedance matching resistors. no external resistors, capacitors or magnetic components are necessary on the d+/d- signal paths between the pic18f1xk50/pic18lf1xk50 family device and usb cable.
? 2010 microchip technology inc. preliminary ds41350e-page 377 pic18f/lf1xk50 27.6 thermal considerations standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c param no. sym characteristic typ units conditions th01 ? ja thermal resistance junction to ambient 62.4 ? c/w 20-pin pdip package 85.2 ? c/w 20-pin soic package 108.1 ? c/w 20-pin ssop package tbd ? c/w 20-pin qfn 5x5mm package th02 ? jc thermal resistance junction to case 31.4 ? c/w 20-pin pdip package 24 ? c/w 20-pin soic package 24 ? c/w 20-pin ssop package th03 t jmax maximum junction temperature 150 ? c th04 pd power dissipation ? w pd = p internal + p i / o th05 p internal internal power dissipation ? w p internal = i dd x v dd (1) th06 p i / o i/o power dissipation ? w p i / o = ? (i ol * v ol ) + ? (i oh * (v dd - v oh )) th07 p der derated power ? w p der = pd max (t j - t a )/ ? ja (2) legend: tbd = to be determined note 1: i dd is current to run the chip alone without driving any load on the output pins. 2: t a = ambient temperature 3: t j = junction temperature
pic18f/lf1xk50 ds41350e-page 378 preliminary ? 2010 microchip technology inc. 27.7 timing parameter symbology the timing parameter symbols have been created with one of the following formats: figure 27-2: load conditions 1. tpps2pps 2. tpps t f frequency t time lowercase letters (pp) and their meanings: pp cc ccp1 osc osc1 ck clkout rd rd cs cs rw rd or wr di sdi sc sck do sdo ss ss dt data in t0 t0cki io i/o port t1 t1cki mc mclr wr wr uppercase letters and their meanings: s ffall pperiod hhigh rrise i invalid (high-impedance) v valid l low z high-impedance v ss c l legend: c l = 50 pf for all pins, 15 pf for osc2 output load condition pin
? 2010 microchip technology inc. preliminary ds41350e-page 379 pic18f/lf1xk50 27.8 ac characteristics: pic18f1xk50/pic18lf1xk50-i/e figure 27-3: clock timing figure 27-4: pic18f1xk50 volt age frequency graph, -40c ? t a ?? +85c osc1/clkin osc2/clkout q4 q1 q2 q3 q4 q1 os02 os03 os04 os04 osc2/clkout (lp,xt,hs modes) (clkout mode) 1.8 0 frequency (mhz) v dd (v) note 1: the shaded region indicates the permissible combinations of voltage and frequency. 2: refer to table 27-1 for each oscillator mode?s supported frequencies. 10 48 20 40 5.5 3.6 2.7
pic18f/lf1xk50 ds41350e-page 380 preliminary ? 2010 microchip technology inc. figure 27-5: pic18lf1xk50 voltage frequency graph, -40c ? t a ?? +125c figure 27-6: hfintosc frequency accuracy over device v dd and temperature 1.8 2.7 0 frequency (mhz) v dd (v) note 1: the shaded region indicates the permissible combinations of voltage and frequency. 2: refer to table 27-1 for each oscillator mode?s supported frequencies. 10 48 20 40 3.6 125 25 2.0 0 60 85 v dd (v) 4.0 5.0 4.5 temperature ( c ) 2.5 3.0 3.5 5.5 1.8 note 1: this chart covers both regulator enabled and regulator disabled states. 2: regulator nominal voltage 3.3 (2) -40 -20 2% 5% 3% 5% 5%
? 2010 microchip technology inc. preliminary ds41350e-page 381 pic18f/lf1xk50 table 27-1: clock oscillator timing requirements standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c param no. sym characteristic min typ? max units conditions os01 f osc external clkin frequency (1) dc ? 37 khz ec oscillator mode (low) dc ? 4 mhz ec oscillator mode (medium) dc ? 48 mhz ec oscillator mode (high) oscillator frequency (1) ? 32.768 ? khz lp oscillator mode 0.1 ? 4 mhz xt oscillator mode 1 ? 20 mhz hs oscillator mode dc ? 4 mhz rc oscillator mode os02 t osc external clkin period (1) 27 ? ?? s lp oscillator mode 250 ? ? ns xt oscillator mode 50 ? ? ns hs oscillator mode 20.80 ? ? ns ec oscillator mode oscillator period (1) ?? ? ? s lp oscillator mode 250 ? 10,000 ns xt oscillator mode 50 ? 1,000 ns hs oscillator mode 250 ? ? ns rc oscillator mode os03 t cy instruction cycle time (1) 83 t cy dc ns t cy = 4/f osc os04* tosh, to sl external clkin high, external clkin low 2?? ? s lp oscillator 100 ? ? ns xt oscillator 20 ? ? ns hs oscillator os05* tosr, to sf external clkin rise, external clkin fall 0? ? ns lp oscillator 0? ? ns xt oscillator 0? ? ns hs oscillator * these parameters are characterized but not tested. ? data in ?typ? column is at 3.0v, 25c unless otherwise stat ed. these parameters are for design guidance only and are not tested. note 1: instruction cycle period (t cy ) equals four times the input oscillator time base period. all specified values are based on characterization data for that particular oscillator type under standard operating conditions with the device executing code. exceeding these specified limits may result in an unstable oscillator operation and/or high er than expected current con- sumption. all devices are tested to operat e at ?min? values with an external clock applied to osc1 pin. when an external clock input is used, the ?max? cycle time limit is ?dc? (no clock) for all devices.
pic18f/lf1xk50 ds41350e-page 382 preliminary ? 2010 microchip technology inc. table 27-2: oscillator parameters table 27-3: pll clock timing specifications (v dd = 42.7v to 5.5v) standard operating conditions (unless otherwise stated) operating temperature -40c ?? t a ?? +125c param no. sym characteristic freq. tolerance min typ? max units conditions os08 hf osc internal calibrated hfintosc frequency (2) ? 2% ? 3% ? ? 16.0 16.0 ? ? mhz mhz 0c ? t a ? +60c 60c ? t a ? +85c ? 5% ? 16.0 ? mhz -40c ? t a ? +125c os10* t iosc st hfintosc wake-up from sleep start-up time ??58 ? sv dd = 2.0v, -40c to +85c ??58 ? sv dd = 3.0v, -40c to +85c ??58 ? sv dd = 5.0v, -40c to +85c * these parameters are characterized but not tested. ? data in ?typ? column is at 3.0v, 25c unless otherwise stated. these parameters are for design guidance only and are not tested. note 1: instruction cycle period (t cy ) equals four times the input oscillator time bas e period. all specified values are based on characterization data for that particular oscillator ty pe under standard operating conditions with the device executing code. exceeding these specified limits may result in an unst able oscillator operation and/or higher than expected current consumption. all devices are tested to operate at ?min? valu es with an external clock applied to the osc1 pin. when an external clock input is used, the ?max? cycle ti me limit is ?dc? (no clock) for all devices. 2: to ensure these oscillator frequency tolerances, v dd and v ss must be capacitively decoupled as close to the device as possible. 0.1 ? f and 0.01 ? f values in parallel are recommended. 3: by design. param no. sym characteristic min typ? max units conditions f10 f osc oscillator frequency range 4 ? 12 mhz f11 f sys on-chip vco system frequency 16 ? 48 mhz f12 t rc pll start-up time (lock time) ? ? 2 ms f13* ? clk clkout stability (jitter) -0.25% ? +0.25% % * these parameters are characterized but not tested. ? data in ?typ? column is at 3.0v, 25 ? c unless otherwise stated. these parameters are for design guidance only and are not tested.
? 2010 microchip technology inc. preliminary ds41350e-page 383 pic18f/lf1xk50 figure 27-7: clkout and i/o timing table 27-4: clkout and i/o timing parameters standard operating conditions (unless otherwise stated) operating temperature -40c ?? t a ?? +125c param no. sym characteristic min typ? max units conditions os11 tosh2ckl fosc ? to clkout ? (1) ??70nsv dd = 3.0-5.0v os12 tosh2ckh fosc ? to clkout ? (1) ??72nsv dd = 3.0-5.0v os13 tckl2iov clkout ? to port out valid (1) ??20ns os14 tiov2ckh port input valid before clkout ? (1) t osc + 200 ns ? ? ns os15 tosh2iov fosc ? (q1 cycle) to port out valid ? 50 70* ns v dd = 3.0-5.0v os16 tosh2ioi fosc ? (q2 cycle) to port input invalid (i/o in hold time) 50 ? ? ns v dd = 3.0-5.0v os17 tiov2osh port input valid to fosc ?? (q2 cycle) (i/o in setup time) 20 ? ? ns os18 tior port output rise time ? ? 90 55 140 80 ns v dd = 2.0v v dd = 3.3-5.0v os19 tiof port output fall time ? ? 60 44 80 60 ns v dd = 2.0v v dd = 3.3-5.0v os20* tinp int pin input high or low time 25 ? ? ns os21* trbp portb interrupt-on-change new input level time t cy ??ns * these parameters are characterized but not tested. ? data in ?typ? column is at 3.0v, 25 ? c unless otherwise stated. note 1: measurements are taken in rc mode where clkout output is 4 x t osc . f osc clkout i/o pin (input) i/o pin (output) q4 q1 q2 q3 os11 os19 os13 os15 os18, os19 os20 os21 os17 os16 os14 os12 os18 old value new value write fetch read execute cycle
pic18f/lf1xk50 ds41350e-page 384 preliminary ? 2010 microchip technology inc. figure 27-8: reset, watchdog timer, os cillator start-up timer and power-up timer timing figure 27-9: brown-out rese t timing and characteristics v dd mclr internal por pwrt time-out osc start-up time internal reset (1) watchdog timer 33 32 30 31/ 34 i/o pins 34 note 1: asserted low. reset (1) 31a v bor v dd (device in brown-out reset) (device not in brown-out reset) 33 (1) 37 note 1: 64 ms delay only if pwrte bit in the configuration word register is programmed to ? 0 ?. 2 ms delay if pwrte = 0 . reset (due to bor) v bor and v hyst tborrej
? 2010 microchip technology inc. preliminary ds41350e-page 385 pic18f/lf1xk50 table 27-5: reset, watchdog timer, oscill ator start-up timer, power-up timer, and brown-out reset parameters standard operating conditions (unless otherwise stated) operating temperature -40c ?? t a ?? +125c param no. sym characteristic min typ? max units conditions 30 t mc lmclr pulse width (low) 2 5 ? ? ? ? ? s ? s v dd = 3.3-5v, -40c to +85c v dd = 3.3-5v 31 t wdt standard watchdog timer time-out period (5) 10 10 17 17 27 30 ms ms v dd = 3.3v-5v, -40c to +85c v dd = 3.3v-5v 31a t wdtlp low power watchdog timer time-out period 10 10 18 18 27 33 ms ms v dd = 3.3v-5v, -40c to +85c v dd = 3.3v-5v 32 t ost oscillator start-up timer period (1), (2) ? 1024 ? tosc (note 3) 33* t pwrt power-up timer period, pwrte = 0 40 65 140 ms 34* t ioz i/o high-impedance from mclr low or watchdog timer reset ??2.0 ? s 35 v bor brown-out reset voltage 1.8 2.09 2.57 2.71 1.9 2.2 2.7 2.85 2.05 2.35 2.85 3.0 v v v v borv = 1.9v borv = 2.2v borv = 2.7v borv = 2.85v 36* v hyst brown-out reset hysteresis 25 50 75 mv -40c to +85c 37* t bordc brown-out reset dc response time 0140 ? sv dd ? v bor legend: tbd = to be determined * these parameters are characterized but not tested. ? data in ?typ? column is at 3v, 25c unless otherwise stated. these parameters are for design guidance only and are not tested. note 1: instruction cycle period (t cy ) equals four times the input oscillator time base period. all specified values are based on characterization data for that particular oscillator type under standard operating conditions with the device executing code. exceeding these specified limits may result in an unstable oscillator operation and/or higher than expected current consumption. all devices are tested to operate at ?min? values with an external clock applied to the osc1 pin. when an external clock input is used, the ?max? cycle time limit is ?dc? (no clock) for all devices. 2: by design. 3: period of the slower clock. 4: to ensure these voltage tolerances, v dd and v ss must be capacitively decoupled as close to the device as possible. 0.1 ? f and 0.01 ? f values in parallel are recommended. 5: design target. if unable to meet this target, the maximum can be increased, but the minimum cannot be changed.
pic18f/lf1xk50 ds41350e-page 386 preliminary ? 2010 microchip technology inc. figure 27-10: timer0 and time r1 external clock timings table 27-6: timer0 and timer1 external clock requirements standard operating conditions (unless otherwise stated) operating temperature -40c ?? t a ?? +125c param no. sym characteristic min typ? max units conditions 40* t t 0h t0cki high pulse width no prescaler 0.5 t cy + 20 ? ? ns with prescaler 10 ? ? ns 41* t t 0l t0cki low pulse width no prescaler 0.5 t cy + 20 ? ? ns with prescaler 10 ? ? ns 42* t t 0p t0cki period greater of: 20 or t cy + 40 n ? ? ns n = prescale value (2, 4, ..., 256) 45* t t 1h t1cki high time synchronous, no prescaler 0.5 t cy + 20 ? ? ns synchro- nous, with prescaler 15 ? ? ns asynchro- nous 30 ? ? ns 46* t t 1l t1cki low time synchronous, no prescaler 0.5 t cy + 20 ? ? ns synchronous, with prescaler 15 ? ? ns asynchronous 30 ? ? ns 47* t t 1p t1cki input period synchronous greater of: 30 or t cy + 40 n ? ? ns n = prescale value (1, 2, 4, 8) asynchronous 60 ? ? ns 48 f t 1 timer1 oscillator input frequency range (oscillator enabled by setting bit t1oscen) 32.4 32.768 33.1 khz 49* tckez tmr 1 delay from external clock edge to timer increment 2 t osc ?7 t osc ? timers in sync mode * these parameters are characterized but not tested. ? data in ?typ? column is at 3v, 25c unless otherwise stated . these parameters are for design guidance only and are not tested. t0cki t1cki 40 41 42 45 46 47 49 tmr0 or tmr1
? 2010 microchip technology inc. preliminary ds41350e-page 387 pic18f/lf1xk50 figure 27-11: capture/com pare/pwm timings (ccp) table 27-7: capture/compare/pwm requirements (ccp) table 27-8: pic18f1xk50/pic18lf1xk50 a/d converter (adc) characteristics : standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c param no. sym characteristic min typ? max units conditions cc01* tccl ccpx input low time no prescaler 0.5t cy + 20 ? ? ns with prescaler 20 ? ? ns cc02* tcch ccpx input high time no prescaler 0.5t cy + 20 ? ? ns with prescaler 20 ? ? ns cc03* tccp ccpx input period 3t cy + 40 n ? ? ns n = prescale value (1, 4 or 16) * these parameters are characterized but not tested. ? data in ?typ? column is at 5v, 25c unless otherwise stated . these parameters are for design guidance only and are not tested. standard operating conditions (unless otherwise stated) operating temperature t a ? 25c param no. sym characteristic min typ? max units conditions ad01 n r resolution ? ? 10 bit ad02 e il integral error ? ? 2 lsb v ref = 3.0v ad03 e dl differential error ? ? 1.5 lsb no missing codes v ref = 3.0v ad04 e off offset error ? ? 3 lsb v ref = 3.0v ad05 e gn gain error ? ? 3 lsb v ref = 3.0v ad06 v ref change in reference voltage = v ref + - v ref - (3) 1.8 ? v dd v1.8 ?? v ref + ?? v dd + 0.3v v ss - 0.3v ?? v ref - ?? v ref + - 1.8v ad07 v ain full-scale range v ss ?v ref v ad08 z ain recommended impedance of analog voltage source ??2.5 k ? can go higher if external 0.01 ? f capacitor is present on input pin. ad09* i ref v ref input current (3) 10 ? 1000 ? a during v ain acquisition. based on differential of v hold to v ain . ?? 10 ? a during a/d conversion cycle. * these parameters are characterized but not tested. ? data in ?typ? column is at 3.0v, 25c unless otherwise st ated. these parameters are for design guidance only and are not tested. note 1: total absolute error includes integral, differential, offset and gain errors. 2: the a/d conversion result never dec reases with an increase in the input voltage and has no missing codes. 3: adc v ref is from external v ref , v dd pin or fvr, whichever is selected as reference input. 4: when adc is off, it will not consum e any current other than leakage current. the power-down current specification includes any such leakage from the adc module. note: refer to figure 27-2 for load conditions. (capture mode) cc01 cc02 cc03 ccpx
pic18f/lf1xk50 ds41350e-page 388 preliminary ? 2010 microchip technology inc. figure 27-12: a/d conversion timing table 27-9: a/d conversion requirements param no. symbol characteristic min max units conditions 130 t ad a/d clock period 0.7 25.0 (1) ? st osc based, v ref ? 3.0v 0.7 1 ? s a/d rc mode 131 t cnv conversion time (not including acquisition time) (2) 11 12 t ad 132 t acq acquisition time (3) 1.4 tbd ? ? ? s ? s -40 ? c to +85 ? c 0 ? c ? to ? +85 ? c 135 t swc switching time from convert ? sample ? (note 4) tbd t dis discharge time 0.2 ? ? s legend: tbd = to be determined note 1: the time of the a/d clock period is dependent on the device frequency and the t ad clock divider. 2: adres register may be read on the following t cy cycle. 3: the time for the holding capacitor to acquire the ?new? input voltage when the voltage changes full scale after the conversion (v dd to v ss or v ss to v dd ). the source impedance ( r s ) on the input channels is 50 ? . 4: on the following cycle of the device clock. 131 130 132 bsf adcon0, go q4 a/d clk a/d data adres adif go sample old_data sampling stopped done new_data (note 2) 987 21 0 note 1: if the a/d clock source is selected as rc, a time of t cy is added before the a/d clock starts. this allows the sleep instruction to be executed. 2: this is a minimal rc delay (typically 100 ns), which also disconnects the holding capacitor from the analog input. .. . . . . t cy
? 2010 microchip technology inc. preliminary ds41350e-page 389 pic18f/lf1xk50 table 27-10: comparator specifications table 27-11: cv ref voltage reference specifications table 27-12: fixed voltage refe rence (fvr) specifications operating conditions: 1.8v < v dd < 3.6v, -40c < t a < +125c (unless otherwise stated). param no. sym characteristics min typ max units comments cm01 v ioff input offset voltage ? ? 7.5 ? 50 80 mv mv high power mode low power mode cm02 v icm input common mode voltage 0 ? v dd v cm03 cmrr common mode rejection ratio 55 ? ? db cm04 t resp response time ? 150 400 ns note 1 cm05 t mc 2 ov comparator mode change to output valid* ?? 10 ? s cm06 c hyster comparator hysteresis ? 65 ? mv * these parameters are characterized but not tested. note 1: response time measured with one comparator input at v dd /2, while the other input transitions from v ss to v dd . operating conditions: 1.8v < v dd < 3.6v, -40c < t a < +125c (unless otherwise stated). param no. sym characteristics min typ max units comments cv01* c lsb step size (2) ? ? v dd /24 v dd /32 ? ? v v low range (vrr = 1 ) high range (vrr = 0 ) cv02* c acc absolute accuracy ? ? ? ? ? 1/4 ?? 1/2 lsb lsb low range (vrr = 1 ) high range (vrr = 0 ) cv03* c r unit resistor value (r) ? 2k ? ? cv04* c st settling time (1) ??10 ? s * these parameters are characterized but not tested. note 1: settling time measured while cvrr = 1 and cvr3:cvr0 transitions from ? 0000 ? to ? 1111 ?. operating conditions: 1.8v < v dd < 5.5v, -40c < t a < +85c (unless otherwise stated). vr voltage reference specifications standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +85c -40c ? t a ? +125c param no. sym. characteristics min. typ. max. units comments d003 v adfvr fixed voltage reference voltage -7 -8 -7 -8 -7 -8 ? ? ? ? ? ? 6 6 6 6 6 6 %1.024v, v dd ? 2.5v, 85c 1.024v, v dd ? 2.5v, 125c 2.048v, v dd ? 2.5v, 85c 2.048v, v dd ? 2.5v, 125c 4.096v, v dd ? 4.75v, 85c 4.096v, v dd ? 4.75v, 125c d003c* tcv fvr temperature coefficient, fixed volt- age reference ? -114 ? ppm/c d003d* ? v fvr / ? v in line regulation, fixed voltage ref- erence ? 0.225 ? %/v d004* s vdd v dd rise rate to ensure internal power-on reset signal 0.05 ? ? v/ms see section 6.1 ?power-on reset (por)? for details. * these parameters are characterized but not tested.
pic18f/lf1xk50 ds41350e-page 390 preliminary ? 2010 microchip technology inc. figure 27-13: usart synchronous transmission (master/slave) timing table 27-13: usart synchronous tran smission requirements figure 27-14: usart synchrono us receive (master/slave) timing table 27-14: usart synchronous rece ive requirements standard operating conditions (unless otherwise stated) operating temperature -40c ?? t a ?? +125c param. no. symbol characteristic min. max. units conditions us120 t ck h2 dt v sync xmit (m aster and s lave ) clock high to data-out valid 3.0-5.5v ? 80 ns 1.8-5.5v ? 100 ns us121 t ckrf clock out rise time and fall time (master mode) 3.0-5.5v ? 45 ns 1.8-5.5v ? 50 ns us122 t dtrf data-out rise time and fall time 3.0-5.5v ? 45 ns 1.8-5.5v ? 50 ns standard operating conditions (unless otherwise stated) operating temperature -40c ?? t a ?? +125c param. no. symbol characteristic min. max. units conditions us125 t dt v2 ckl sync rcv (m aster and s lave ) data-hold before ck ? (dt hold time) 10 ? ns us126 t ck l2 dtl data-hold after ck ? (dt hold time) 15 ? ns note: refer to figure 27-2 for load conditions. us121 us121 us120 us122 ck dt note: refer to figure 27-2 for load conditions. us125 us126 ck dt
? 2010 microchip technology inc. preliminary ds41350e-page 391 pic18f/lf1xk50 figure 27-15: spi master mode timing (cke = 0 , smp = 0 ) figure 27-16: spi master mode timing (cke = 1 , smp = 1 ) ss sck (ckp = 0 ) sck (ckp = 1 ) sdo sdi sp70 sp71 sp72 sp73 sp74 sp75, sp76 sp78 sp79 sp80 sp79 sp78 msb lsb bit 6 - - - - - -1 msb in lsb in bit 6 - - - -1 note: refer to figure 27-2 for load conditions. ss sck (ckp = 0 ) sck (ckp = 1 ) sdo sdi sp81 sp71 sp72 sp74 sp75, sp76 sp78 sp80 msb sp79 sp73 msb in bit 6 - - - - - -1 lsb in bit 6 - - - -1 lsb note: refer to figure 27-2 for load conditions.
pic18f/lf1xk50 ds41350e-page 392 preliminary ? 2010 microchip technology inc. figure 27-17: spi slav e mode timing (cke = 0 ) figure 27-18: spi slav e mode timing (cke = 1 ) ss sck (ckp = 0 ) sck (ckp = 1 ) sdo sdi sp70 sp71 sp72 sp73 sp74 sp75, sp76 sp77 sp78 sp79 sp80 sp79 sp78 msb lsb bit 6 - - - - - -1 msb in bit 6 - - - -1 lsb in sp83 note: refer to figure 27-2 for load conditions. ss sck (ckp = 0 ) sck (ckp = 1 ) sdo sdi sp70 sp71 sp72 sp82 sp74 sp75, sp76 msb bit 6 - - - - - -1 lsb sp77 msb in bit 6 - - - -1 lsb in sp80 sp83 note: refer to figure 27-2 for load conditions.
? 2010 microchip technology inc. preliminary ds41350e-page 393 pic18f/lf1xk50 table 27-15: spi mode requirements figure 27-19: i 2 c? bus start/stop bits timing param no. symbol characteristic min typ? max units conditions sp70* t ss l2 sc h, t ss l2 sc l ss ? to sck ? or sck ? input t cy ??ns sp71* t sc h sck input high time (slave mode) t cy + 20 ? ? ns sp72* t sc l sck input low time (slave mode) t cy + 20 ? ? ns sp73* t di v2 sc h, t di v2 sc l setup time of sdi data input to sck edge 100 ? ? ns sp74* t sc h2 di l, t sc l2 di l hold time of sdi data input to sck edge 100 ? ? ns sp75* t do r sdo data output rise time 3.0-5.5v ? 10 25 ns 1.8-5.5v ? 25 50 ns sp76* t do f sdo data output fall time ? 10 25 ns sp77* t ss h2 do zss ? to sdo output high-impedance 10 ? 50 ns sp78* t sc r sck output rise time (master mode) 3.0-5.5v ? 10 25 ns 1.8-5.5v ? 25 50 ns sp79* t sc f sck output fall time (master mode) ? 10 25 ns sp80* t sc h2 do v, t sc l2 do v sdo data output valid after sck edge 3.0-5.5v ? ? 50 ns 1.8-5.5v ? ? 145 ns sp81* t do v2 sc h, t do v2 sc l sdo data output setup to sck edge tcy ? ? ns sp82* t ss l2 do v sdo data output valid after ss ? edge ? ? 50 ns sp83* t sc h2 ss h, t sc l2 ss h ss ?? after sck edge 1.5t cy + 40 ? ? ns * these parameters are characterized but not tested. ? data in ?typ? column is at 3.0v, 25c unless otherwise stated. these parameters are for design guidance only and are not tested. note : refer to figure 27-2 for load conditions. sp91 sp92 sp93 scl sda start condition stop condition sp90
pic18f/lf1xk50 ds41350e-page 394 preliminary ? 2010 microchip technology inc. table 27-16: i 2 c? bus start/stop bits requirements figure 27-20: i 2 c? bus data timing param no. symbol characteristic min typ max units conditions sp90* t su : sta start condition 100 khz mode 4700 ? ? ns only relevant for repeated start condition setup time 400 khz mode 600 ? ? sp91* t hd : sta start condition 100 khz mode 4000 ? ? ns after this period, the first clock pulse is generated hold time 400 khz mode 600 ? ? sp92* t su : sto stop condition 100 khz mode 4700 ? ? ns setup time 400 khz mode 600 ? ? sp93 t hd : sto stop condition 100 khz mode 4000 ? ? ns hold time 400 khz mode 600 ? ? * these parameters are characterized but not tested. note: refer to figure 27-2 for load conditions. sp90 sp91 sp92 sp100 sp101 sp103 sp106 sp107 sp109 sp109 sp110 sp102 scl sda in sda out
? 2010 microchip technology inc. preliminary ds41350e-page 395 pic18f/lf1xk50 table 27-17: i 2 c? bus data requirements param. no. symbol characteristic min max units conditions sp100* t high clock high time 100 khz mode 4.0 ? ? s device must operate at a minimum of 1.5 mhz 400 khz mode 0.6 ? ? s device must operate at a minimum of 10 mhz ssp module 1.5t cy ? sp101* t low clock low time 100 khz mode 4.7 ? ? s device must operate at a minimum of 1.5 mhz 400 khz mode 1.3 ? ? s device must operate at a minimum of 10 mhz ssp module 1.5t cy ? sp102* t r sda and scl rise time 100 khz mode ? 1000 ns 400 khz mode 20 + 0.1c b 300 ns c b is specified to be from 10-400 pf sp103* t f sda and scl fall time 100 khz mode ? 250 ns 400 khz mode 20 + 0.1c b 250 ns c b is specified to be from 10-400 pf sp90* t su : sta start condition setup time 100 khz mode 4.7 ? ? s only relevant for repeated start condition 400 khz mode 0.6 ? ? s sp91* t hd : sta start condition hold time 100 khz mode 4.0 ? ? s after this period the first clock pulse is generated 400 khz mode 0.6 ? ? s sp106* t hd : dat data input hold time 100 khz mode 0 ? ns 400 khz mode 0 0.9 ? s sp107* t su : dat data input setup time 100 khz mode 250 ? ns (note 2) 400 khz mode 100 ? ns sp92* t su : sto stop condition setup time 100 khz mode 4.7 ? ? s 400 khz mode 0.6 ? ? s sp109* t aa output valid from clock 100 khz mode ? 3500 ns (note 1) 400 khz mode ? ? ns sp110* t buf bus free time 100 khz mode 4.7 ? ? s time the bus must be free before a new transmission can start 400 khz mode 1.3 ? ? s sp c b bus capacitive loading ? 400 pf * these parameters are characterized but not tested. note 1: as a transmitter, the device must provide this internal minimum delay time to bridge the undefined region (min. 300 ns) of the falling edge of scl to avoid unintended generation of start or stop conditions. 2: a fast mode (400 khz) i 2 c bus device can be used in a standard mode (100 khz) i 2 c bus system, but the requirement t su : dat ?? 250 ns must then be met. this will automatically be the case if the device does not stretch the low period of the scl signal. if such a device does stretch the low period of the scl signal, it must output the next data bit to the sda line t r max. + t su : dat = 1000 + 250 = 1250 ns (according to the standard mode i 2 c bus specification), before the scl line is released.
pic18f/lf1xk50 ds41350e-page 396 preliminary ? 2010 microchip technology inc. notes:
? 2010 microchip technology inc. preliminary ds41350e-page 397 pic18f/lf1xk50 28.0 dc and ac characteristics graphs and tables graphs and tables are not available at this time.
pic18f/lf1xk50 ds41350e-page 398 preliminary ? 2010 microchip technology inc. notes:
? 2010 microchip technology inc. preliminary ds41350e-page 399 pic18f/lf1xk50 29.0 packaging information 29.1 package marking information legend: xx...x customer-specific information y year code (last digit of calendar year) yy year code (last 2 digits of calendar year) ww week code (week of january 1 is week ?01?) nnn alphanumeric traceability code pb-free jedec designator for matte tin (sn) * this package is pb-free. the pb-free jedec designator ( ) can be found on the outer packaging for this package. note : in the event the full microchip part number cannot be marked on one line, it will be carried over to the next line, thus limiting the number of available characters for customer-specific information. 3 e 3 e 20-lead pdip xxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxx yywwnnn example picxxfxxxx-i/p 0810017 20-lead ssop xxxxxxxxxxx xxxxxxxxxxx yywwnnn example picxxfxxxx -i/ss 0810017 20-lead soic (.300?) xxxxxxxxxxxxxx xxxxxxxxxxxxxx xxxxxxxxxxxxxx yywwnnn example picxxfxxxx-i /so 0810017 xxxxxxx 20-lead qfn xxxxxxx yywwnnn 18f14k50 example -i/ml 0810017
pic18f/lf1xk50 ds41350e-page 400 preliminary ? 2010 microchip technology inc. 29.2 package details the following sections give the technical details of the packages. 
    
     

       !" # $% &" '  ()"&'"!&)  & #*&&  & #    + % &,  & !& - ' !! #.#&"# '#% !
&"!!#% !
&"!!! & $ #/
!#   ' !  #&   
.0 1,2 1 !' !  &  $ & " !**&"&&   !   3& '!&" &
4 # * !(
 ! ! & 
 4  
% & & # & &&
255***'
'5
4  6&! 7,8. ' !9'&! 7 7: ; 7"') %! 7  & 1, 
& &    < <  # # 4 4 !!   -  1 ! & &     < < "# &"# =#& . - - - # # 4 =#& .   > :  9  &  > - ? 
& &   9  -  9 #4 !!  >   6
9 #=#& )  ?  9* 9 #=#& )  >  :  *
 + 1 < < - n e1 note 1 d 123 a a1 a2 l e b1 b e c eb 
    * , 1
? 2010 microchip technology inc. preliminary ds41350e-page 401 pic18f/lf1xk50 
  !"#  $     %&    $
      !" # $% &" '  ()"&'"!&)  & #*&&  & #    ' !! #.#&"# '#% !
&"!!#% !
&"!!! & $ #''
!#  - ' !  #&   
.0 1,2 1 !' !  &  $ & " !**&"&&   ! .32  %   ' !("!" *&"&&   (%%' &
"
! !   3& '!&" &
4 # * !(
 ! ! & 
 4  
% & & # & &&
255***'
'5
4  6&! 99.. ' !9'&! 7 7: ; 7"') %! 7  & ?1, :  8  &  < <  # # 4 4 !!  ?  > & #%%   < < :  =#& .  > > # # 4 =#& .  - ? :  9  &  ?   3&9  & 9    3&
& 9 .3 9 #4 !!   <  3&  @ @ >@ 9 #=#& )  < -> l l1 a2 c e b a1 a 12 note 1 e1 e d n 
    * , 1
pic18f/lf1xk50 ds41350e-page 402 preliminary ? 2010 microchip technology inc. 
   $   $  '( )&%    $*      !" # $% &" '  ()"&'"!&)  & #*&&  & #    + % &,  & !& - ' !! #.#&"# '#% !
&"!!#% !
&"!!! & $ #''
!#   ' !  #&   
.0 1,2 1 !' !  &  $ & " !**&"&&   ! .32  %   ' !("!" *&"&&   (%%' &
"
! !   3& '!&" &
4 # * !(
 ! ! & 
 4  
% & & # & &&
255***'
'5
4  6&! 99.. ' !9'&! 7 7: ; 7"') %! 7  & 1, :  8  &  < < ? # # 4 4 !!   < < & #%%+   < - :  =#& . -1, # # 4 =#& . 1, :  9  &  >1, , '% a
& b   <  3&9  & 9  <  3&
& 9 .3 3&  @ < >@ 9 #4 !!   < -- 9 #=#& ) - <  # %&  
@ < @ # %&  1&&' @ < @ d e e1 e b 123 note 1 a a1 a2 h h c l1 l n 
    * , 1
? 2010 microchip technology inc. preliminary ds41350e-page 403 pic18f/lf1xk50 20-lead plastic quad flat, no lead package (mq) ? 5x5x0.9 mm body [qfn] note: for the most current package drawings, please see the microchip packaging specification located at http://www.microchip.com/packaging microchip technology drawing c04-120a
pic18f/lf1xk50 ds41350e-page 404 preliminary ? 2010 microchip technology inc. notes:
? 2010 microchip technology inc. preliminary ds41350e-page 405 pic18f/lf1xk50 appendix a: revision history revision a (may 2008) original data sheet for pic18f1xk50/pic18lf1xk50 devices. revision b (june 2008) revised 27.4 dc characteristics table. revision c (04/2009) revised data sheet title; revised features section; revised table 1-2; revised table 3-1, table 3-2; added note 3 in section 9.1; revised register 14-1; revised example 16-1; revised section 18.8.4; revised register 18-3; revised table 20-2; revised sections 22.2.1, 22.2.2, 22.5.1.1, 22.7; revised tables 23-4, 27-1, 27-2, 27-3 27-4, 27-8. revision d (05/2010) revised the 20-pin pdip, ssop, soic diagram; added the 20-pin qfn diagram; revised table 1, table 1-1; revised figure 2-1; added note below sec- tion 2.11.1 (low speed operation); revised table 3-1, table 3-2; revised section 4 (flash program memory) and section 5 (data eeprom memory); revised example 5-2, table 5-1; deleted note 1 from registers 7-4, 7-8; revised tables 9-1, 9-3; revised sections 14.1 (eccp outputs and configuration), 14.4.4 (enhanced pwm auto-shutdown mode); added note 4 below register 14-2; revised figure 14-10; revised equation 17-1; revised table 18-3 and table 20-3; revised equation 21-1; deleted section 21.1.3 (output clamped to v ss ); revised figure 21-1; revised table 21-1, table 23-4 and table 24-1; added note 2 to table 24-1; revised register 24-6; deleted note 1 from table 24-3; revised section 27 (tables); added 20- lead qfn package marking information and package details; revised the product identification system section; other minor corrections. revision e (10/2010) updated section 27.0 electrical specifications.
pic18f/lf1xk50 ds41350e-page 406 preliminary ? 2010 microchip technology inc. appendix b: device differences the differences between the devices listed in this data sheet are shown in tab le b - 1 . table b-1: device differences features pic18f13k50 pic18f14k50 pic18lf13k50 pic18f26k20 pic18lf14k50 pic18f44k20 pic18f45k20 pic18f46k20 program memory (bytes) 8192 16384 32768 65536 8192 16384 32768 65536 program memory (instructions) 4096 8192 16384 32768 4096 8192 16384 32768 interrupt sources1919 19 19 20 202020 i/o ports ports a, b, c, (e) ports a, b, c, (e) ports a, b, c, (e) ports a, b, c, (e) ports a, b, c, d, e ports a, b, c, d, e ports a, b, c, d, e ports a, b, c, d, e capture/compare/ pwm modules 11 1 1 1 111 enhanced capture/compare/ pwm modules 11 1 1 1 111 parallel communications (psp) no no no no yes yes yes yes 10-bit analog-to- digital module 11 input channels 11 input channels 11 input channels 11 input channels 14 input channels 14 input channels 14 input channels 14 input channels packages 20-pin pdip 20-pin soic 20-pin ssop 20-pin qfn 20-pin pdip 20-pin soic 20-pin ssop 20-pin qfn 20-pin pdip 20-pin soic 20-pin ssop 20-pin qfn 28-pin pdip 28-pin soic 28-pin ssop 28-pin qfn 20-pin pdip 20-pin soic 20-pin ssop 20-pin qfn 40-pin pdip 44-pin tqfp 44-pin qfn 40-pin pdip 44-pin tqfp 44-pin qfn 40-pin pdip 44-pin tqfp 44-pin qfn
? 2010 microchip technology inc. preliminary ds41350e-page 407 pic18f/lf1xk50 index a a/d analog port pins, configuring .................................. 221 associated registers ............................................... 221 conversions ............................................................. 212 discharge ................................................................. 213 selecting and configuring acquisition time ............ 210 specifications ........................................................... 387 absolute maximum ratings ............................................. 363 ac characteristics industrial and extended ........................................... 379 load conditions ....................................................... 378 access bank mapping with indexed literal offset mode ................. 50 ackstat ........................................................................ 171 ackstat status flag ..................................................... 171 adc ................................................................................. 209 acquisition requirements ........................................ 219 block diagram .......................................................... 209 calculating acquisition time .................................... 219 channel selection .................................................... 210 configuration ............................................................ 210 conversion clock ..................................................... 210 conversion procedure ............................................. 214 internal sampling switch (r ss ) i mpedance ............. 219 interrupts .................................................................. 211 operation ................................................................. 212 operation during sleep ........................................... 213 port configuration .................................................... 210 power management ................................................. 213 reference voltage (v ref ) ........................................ 210 result formatting ..................................................... 211 source impedance ................................................... 219 special event trigger ............................................... 213 starting an a/d conversion ..................................... 211 adcon0 register ............................................................ 215 adcon1 register .................................................... 216, 217 addfsr .......................................................................... 352 addlw ............................................................................ 315 addulnk ........................................................................ 352 addwf ............................................................................ 315 addwfc ......................................................................... 316 adresh register (adfm = 0) ........................................ 218 adresh register (adfm = 1) ........................................ 218 adresl register (adfm = 0) ......................................... 218 adresl register (adfm = 1) ......................................... 218 analog input connection considerations ......................... 231 analog-to-digital converter. see adc andlw ............................................................................ 316 andwf ............................................................................ 317 ansel (port analog control) ......................................... 98 ansel register ................................................................. 98 anselh register .............................................................. 99 assembler mpasm assembler .................................................. 360 b bank select register (bsr) ............................................... 35 baud rate generator ....................................................... 167 baudcon register ......................................................... 192 bc .................................................................................... 317 bcf .................................................................................. 318 bf .................................................................................... 171 bf status flag ................................................................. 171 block diagrams adc ......................................................................... 209 adc transfer function ............................................ 220 analog input model .......................................... 220, 231 baud rate generator .............................................. 167 capture mode operation ......................................... 119 clock source ............................................................. 16 comparator 1 ........................................................... 224 comparator 2 ........................................................... 225 crystal operation ....................................................... 17 eusart receive .................................................... 182 eusart transmit ................................................... 181 external por circuit (slow v dd power-up) ............ 279 external rc mode ..................................................... 18 fail-safe clock monitor (fscm) ................................ 26 generic i/o port ......................................................... 83 interrupt logic ............................................................ 69 mssp (i 2 c master mode) ........................................ 165 mssp (i 2 c mode) .................................................... 148 mssp (spi mode) ................................................... 139 on-chip reset circuit .............................................. 277 pic18f1xk50/pic18lf1xk50 .................................. 12 pwm (enhanced) .................................................... 121 reads from flash program memory ......................... 55 resonator operation ................................................. 18 table read operation ............................................... 51 table write operation ............................................... 52 table writes to flash program memory .................... 57 timer0 in 16-bit mode ............................................. 103 timer0 in 8-bit mode ............................................... 102 timer1 ..................................................................... 106 timer1 (16-bit read/write mode) ............................ 106 timer2 ..................................................................... 112 timer3 ..................................................................... 114 timer3 (16-bit read/write mode) ............................ 115 usb interrupt logic ................................................. 265 usb peripheral and options ................................... 251 voltage reference ................................................... 246 voltage reference output buffer example ............. 247 watchdog timer ...................................................... 302 bn .................................................................................... 318 bnc ................................................................................. 319 bnn ................................................................................. 319 bnov .............................................................................. 320 bnz ................................................................................. 320 bor. see brown-out reset. bov ................................................................................. 323 bra ................................................................................. 321 break character (12-bit) transmit and receive .............. 200 brg. see baud rate generator. brown-out reset (bor) ................................................... 280 detecting ................................................................. 280 disabling in sleep mode .......................................... 280 software enabled .................................................... 280 specifications .......................................................... 385 timing and characteristics ...................................... 384 bsf .................................................................................. 321 btfsc ............................................................................. 322 btfss ............................................................................. 322 btg ................................................................................. 323 bz .................................................................................... 324
pic18f/lf1xk50 ds41350e-page 408 preliminary ? 2010 microchip technology inc. c c compilers mplab c18 ............................................................. 360 call ................................................................................ 324 callw ............................................................................. 353 capture (ccp module) ..................................................... 119 ccp pin configuration ............................................. 119 ccprxh:ccprxl registers ................................... 119 prescaler .................................................................. 119 software interrupt .................................................... 119 timer1/timer3 mode selection ................................ 119 capture/compare/pwm (ccp) capture mode. see capture. ccp mode and timer resources ............................ 118 compare mode. see compare. ccp1con register ......................................................... 117 clock accuracy with asynchronous operation ................ 190 clock sources associated registers ................................................... 27 external modes hs ...................................................................... 17 lp ....................................................................... 17 xt ...................................................................... 17 clrf ................................................................................ 325 clrwdt .......................................................................... 325 cm1con0 register ......................................................... 229 cm2con0 register ......................................................... 230 cm2con1 register ......................................................... 233 code examples 16 x 16 signed multiply routine ................................ 66 16 x 16 unsigned multiply routine ............................ 66 8 x 8 signed multiply routine .................................... 65 8 x 8 unsigned multiply routine ................................ 65 a/d conversion ........................................................ 214 changing between capture prescalers ................... 119 clearing ram using indirect addressing ................... 46 computed goto using an offset value ................... 32 data eeprom read ................................................. 63 data eeprom refresh routine ................................ 64 data eeprom write ................................................. 63 erasing a flash program memory row ..................... 56 fast register stack .................................................... 32 implementing a timer1 real-time clock ................. 109 initializing porta ...................................................... 84 initializing portb ...................................................... 89 initializing portc ...................................................... 94 loading the sspbuf (sspsr) register ................. 142 reading a flash program memory word .................. 55 saving status, wreg and bsr registers in ram ... 80 writing to flash program memory ....................... 58?59 code protection ............................................................... 291 comf ............................................................................... 326 comparator associated registers ............................................... 234 operation ................................................................. 223 operation during sleep ........................................... 228 response time ........................................................ 226 comparator module ......................................................... 223 c1 output state versus input conditions ................ 226 comparator specifications ............................................... 389 comparator voltage reference (cv ref ) associated registers ............................................... 249 effects of a reset ............................................. 228, 245 operation during sleep ........................................... 245 overview .................................................................. 245 comparator voltage reference (cv ref ) response time ........................................................ 226 comparators effects of a reset .................................................... 228 compare (ccp module) .................................................. 120 ccprx register ...................................................... 120 pin configuration ..................................................... 120 software interrupt .................................................... 120 special event trigger ...................................... 116, 120 timer1/timer3 mode selection ................................ 120 computed goto ............................................................... 32 config1h register ................................................ 293, 294 config1l register ........................................................ 293 config2h register ........................................................ 296 config2l register ........................................................ 295 config3h register ........................................................ 297 config4l register ........................................................ 297 config5h register ........................................................ 298 config5l register ........................................................ 298 config6h register ........................................................ 299 config6l register ........................................................ 299 config7h register ........................................................ 300 config7l register ........................................................ 300 configuration bits ............................................................ 292 configuration register protection .................................... 307 context saving during interrupts ....................................... 80 cpfseq .......................................................................... 326 cpfsgt .......................................................................... 327 cpfslt ........................................................................... 327 customer change notification service ............................ 417 customer notification service ......................................... 417 customer support ............................................................ 417 cv ref voltage reference specifications ........................ 389 d data addressing modes .................................................... 46 comparing addressing modes with the extended in- struction set enabled ........................................ 49 direct ......................................................................... 46 indexed literal offset ................................................ 48 instructions affected .......................................... 48 indirect ....................................................................... 46 inherent and literal .................................................... 46 data eeprom code protection ....................................................... 307 data eeprom memory ..................................................... 61 associated registers ................................................. 64 eeadr register ........................................................ 61 eecon1 and eecon2 registers ............................. 61 operation during code-protect ................................. 64 protection against spurious write ............................. 64 reading ..................................................................... 63 using ......................................................................... 64 write verify ................................................................ 63 writing ....................................................................... 63 data memory ..................................................................... 35 access bank .............................................................. 39 and the extended instruction set .............................. 48 bank select register (bsr) ...................................... 35 general purpose registers ....................................... 39 map for pic18f13k50/pic18lf13k50 ..................... 36 map for pic18f14k50/pic18lf14k50 ..................... 37 special function registers ........................................ 39 usb ram .................................................................. 35 daw ................................................................................ 328 dc and ac characteristics
? 2010 microchip technology inc. preliminary ds41350e-page 409 pic18f/lf1xk50 graphs and tables .................................................. 397 dc characteristics extended and industrial ........................................... 374 industrial and extended ........................................... 364 dcfsnz .......................................................................... 329 decf ............................................................................... 328 decfsz ........................................................................... 329 development support ...................................................... 359 device differences ........................................................... 406 device overview .................................................................. 9 details on individual family members ....................... 10 features (28-pin devices) ......................................... 11 new core features ...................................................... 9 other special features .............................................. 10 device reset timers ........................................................ 281 oscillator start-up timer (ost) ............................... 281 pll lock time-out ................................................... 281 power-up timer (pwrt) ......................................... 281 time-out sequence .................................................. 281 devid1 register .............................................................. 301 devid2 register .............................................................. 301 direct addressing ............................................................... 47 e eccpas register ............................................................ 129 eecon1 register ........................................................ 53, 62 effect on standard pic instructions ................................. 356 electrical specifications ................................................... 363 enhanced capture/compare/pwm (eccp) .................... 117 associated registers ............................................... 138 enhanced pwm mode ............................................. 121 auto-restart ..................................................... 131 auto-shutdown ................................................. 129 direction change in full-bridge output mode . 127 full-bridge application ..................................... 125 full-bridge mode ............................................. 125 half-bridge application .................................... 124 half-bridge application examples ................... 132 half-bridge mode ............................................. 124 output relationships (active-high and active-low) .................................................................. 122 output relationships diagram ......................... 123 programmable dead band delay .................... 132 shoot-through current ..................................... 132 start-up considerations ................................... 128 outputs and configuration ....................................... 118 specifications ........................................................... 387 enhanced universal synchronous asynchronous receiver transmitter (eusart) ............................................. 181 equations estimating usb transceiver current consumption . 273 errata ................................................................................... 7 eusart .......................................................................... 181 asynchronous mode ................................................ 183 12-bit break transmit and receive ................. 200 associated registers, receive ........................ 189 associated registers, transmit ....................... 185 auto-wake-up on break .................................. 198 baud rate generator (brg) ........................... 193 clock accuracy ................................................ 190 receiver ........................................................... 186 setting up 9-bit mode with address detect ...... 188 transmitter ....................................................... 183 baud rate generator (brg) associated registers ....................................... 193 auto baud rate detect .................................... 197 baud rate error, calculating ........................... 193 baud rates, asynchronous modes ................. 194 formulas .......................................................... 193 high baud rate select (brgh bit) ................. 193 clock polarity synchronous mode .......................................... 201 data polarity asynchronous receive .................................... 186 asynchronous transmit ................................... 183 synchronous mode .......................................... 201 interrupts asynchronous receive .................................... 187 asynchronous transmit ................................... 184 synchronous master mode .............................. 201, 205 associated registers, receive ........................ 205 associated registers, transmit ............... 203, 206 reception ........................................................ 203 transmission ................................................... 201 synchronous slave mode associated registers, receive ........................ 207 reception ........................................................ 206 transmission ................................................... 205 extended instruction set addfsr .................................................................. 352 addulnk ............................................................... 352 and using mplab tools ......................................... 358 callw .................................................................... 353 considerations for use ............................................ 356 movsf .................................................................... 353 movss .................................................................... 354 pushl ..................................................................... 354 subfsr .................................................................. 355 subulnk ................................................................ 355 syntax ...................................................................... 351 f fail-safe clock monitor ............................................. 26, 291 fail-safe condition clearing ...................................... 27 fail-safe detection .................................................... 26 fail-safe operation ................................................... 26 reset or wake-up from sleep ................................... 27 fast register stack ........................................................... 32 firmware instructions ...................................................... 309 flash program memory ..................................................... 51 associated registers ................................................. 59 control registers ....................................................... 52 eecon1 and eecon2 ..................................... 52 tablat (table latch) register ........................ 54 tblptr (table pointer) register ...................... 54 erase sequence ........................................................ 56 erasing ...................................................................... 56 operation during code-protect ................................. 59 reading ..................................................................... 55 table pointer boundaries based on operation ....................... 54 table pointer boundaries .......................................... 54 table reads and table writes .................................. 51 write sequence ......................................................... 57 writing to .................................................................. 57 protection against spurious writes ................... 59 unexpected termination ................................... 59 write verify ........................................................ 59 g general call address support ......................................... 164 goto .............................................................................. 330
pic18f/lf1xk50 ds41350e-page 410 preliminary ? 2010 microchip technology inc. h hardware multiplier ............................................................ 65 introduction ................................................................ 65 operation ................................................................... 65 performance comparison .......................................... 65 i i/o ports ............................................................................. 83 i 2 c associated registers ............................................... 180 i 2 c mode (mssp) acknowledge sequence timing ............................... 174 baud rate generator ............................................... 167 bus collision during a repeated start condition .................. 178 during a stop condition ................................... 179 clock arbitration ....................................................... 168 clock stretching ....................................................... 160 10-bit slave receive mode (sen = 1) ............. 160 10-bit slave transmit mode ............................. 160 7-bit slave receive mode (sen = 1) ............... 160 7-bit slave transmit mode ............................... 160 clock synchronization and the ckp bit (sen = 1) .. 161 effects of a reset ..................................................... 175 general call address support ................................. 164 i 2 c clock rate w/brg ............................................. 167 master mode ............................................................ 165 operation ......................................................... 166 reception ......................................................... 171 repeated start condition timing ..................... 170 start condition timing ..................................... 169 transmission .................................................... 171 multi-master communication, bus collision and arbitra- tion ................................................................... 175 multi-master mode ................................................... 175 operation ................................................................. 152 read/write bit information (r/w bit) ............... 152, 153 registers .................................................................. 148 serial clock (rc3/sck/scl) ................................... 153 slave mode .............................................................. 152 addressing ....................................................... 152 reception ......................................................... 153 transmission .................................................... 153 sleep operation ....................................................... 175 stop condition timing .............................................. 174 id locations ............................................................. 291, 307 incf ................................................................................. 330 incfsz ............................................................................ 331 in-circuit debugger .......................................................... 307 in-circuit serial programming (icsp) ...................... 291, 307 indexed literal offset addressing and standard pic18 instructions ............................. 356 indexed literal offset mode ............................................. 356 indirect addressing ............................................................ 47 infsnz ............................................................................ 331 initialization conditions for all registers .................. 285?289 instruction cycle ................................................................. 33 clocking scheme ....................................................... 33 instruction flow/pipelining ................................................. 33 instruction set .................................................................. 309 addlw .................................................................... 315 addwf .................................................................... 315 addwf (indexed literal offset mode) .................... 357 addwfc ................................................................. 316 andlw .................................................................... 316 andwf .................................................................... 317 bc ............................................................................ 317 bcf ......................................................................... 318 bn ............................................................................ 318 bnc ......................................................................... 319 bnn ......................................................................... 319 bnov ...................................................................... 320 bnz ......................................................................... 320 bov ......................................................................... 323 bra ......................................................................... 321 bsf .......................................................................... 321 bsf (indexed literal offset mode) .......................... 357 btfsc ..................................................................... 322 btfss ..................................................................... 322 btg ......................................................................... 323 bz ............................................................................ 324 call ........................................................................ 324 clrf ....................................................................... 325 clrwdt ................................................................. 325 comf ...................................................................... 326 cpfseq .................................................................. 326 cpfsgt .................................................................. 327 cpfslt ................................................................... 327 daw ........................................................................ 328 dcfsnz .................................................................. 329 decf ....................................................................... 328 decfsz .................................................................. 329 extended instruction set ......................................... 351 general format ........................................................ 311 goto ...................................................................... 330 incf ........................................................................ 330 incfsz .................................................................... 331 infsnz .................................................................... 331 iorlw ..................................................................... 332 iorwf ..................................................................... 332 lfsr ....................................................................... 333 movf ...................................................................... 333 movff .................................................................... 334 movlb .................................................................... 334 movlw ................................................................... 335 movwf ................................................................... 335 mullw .................................................................... 336 mulwf .................................................................... 336 negf ....................................................................... 337 nop ......................................................................... 337 opcode field descriptions ....................................... 310 pop ......................................................................... 338 push ....................................................................... 338 rcall ..................................................................... 339 reset ..................................................................... 339 retfie .................................................................... 340 retlw .................................................................... 340 return .................................................................. 341 rlcf ....................................................................... 341 rlncf ..................................................................... 342 rrcf ....................................................................... 342 rrncf .................................. .................................. 343 setf ....................................................................... 343 setf (indexed literal offset mode) ........................ 357 sleep ..................................................................... 344 subfwb ................................................................. 344 sublw .................................................................... 345 subwf .................................................................... 345 subwfb ................................................................. 346 swapf .................................................................... 346
? 2010 microchip technology inc. preliminary ds41350e-page 411 pic18f/lf1xk50 tblrd ..................................................................... 347 tblwt ..................................................................... 348 tstfsz ................................................................... 349 xorlw .................................................................... 349 xorwf .................................................................... 350 intcon register ............................................................... 70 intcon registers ....................................................... 70?72 intcon2 register ............................................................. 71 intcon3 register ............................................................. 72 inter-integrated circuit. see i 2 c. internal oscillator block intosc specifications ........................................... 382, 383 internal rc oscillator use with wdt .......................................................... 302 internal sampling switch (r ss ) i mpedance ..................... 219 internet address ............................................................... 417 interrupt sources ............................................................. 291 adc ......................................................................... 211 capture complete (ccp) ......................................... 119 compare complete (ccp) ....................................... 120 interrupt-on-change (rb7:rb4) .......................... 83, 89 intn pin ..................................................................... 80 portb, interrupt-on-change .................................... 80 tmr0 ......................................................................... 80 tmr0 overflow ........................................................ 103 tmr1 overflow ........................................................ 105 tmr3 overflow ................................................ 113, 115 interrupts ............................................................................ 67 intosc specifications ............................................ 382, 383 ioca register .................................................................... 86 iocb register .................................................................... 91 iorlw ............................................................................. 332 iorwf ............................................................................. 332 ipr registers ..................................................................... 77 ipr1 register ..................................................................... 77 ipr2 register ..................................................................... 78 l lata register .................................................................... 86 latb register .................................................................... 91 latc register ................................................................... 95 lfsr ................................................................................ 333 load conditions ............................................................... 378 low-voltage icsp programming. see single-supply icsp programming m master clear (mclr ) ....................................................... 279 master synchronous serial port (mssp). see mssp. memory organization ......................................................... 29 data memory ............................................................. 35 program memory ....................................................... 29 microchip internet web site ............................................. 417 movf ............................................................................... 333 movff ............................................................................ 334 movlb ............................................................................ 334 movlw ........................................................................... 335 movsf ............................................................................ 353 movss ............................................................................ 354 movwf ........................................................................... 335 mplab asm30 assembler, linker, librarian .................. 360 mplab integrated development environment software . 359 mplab pm3 device programmer ................................... 362 mplab real ice in-circuit emulator system ................ 361 mplink object linker/mplib object librarian ............... 360 mssp ack pulse ....................................................... 152, 153 i 2 c mode. see i 2 c mode. module overview ..................................................... 139 spi mode. see spi mode. sspbuf register .................................................... 144 sspsr register ...................................................... 144 mullw ............................................................................ 336 mulwf ............................................................................ 336 n negf ............................................................................... 337 nop ................................................................................. 337 o osccon register ....................................................... 20, 21 oscillator module ............................................................... 15 oscillator parameters ...................................................... 382 oscillator selection .......................................................... 291 oscillator specifications ................................................... 381 oscillator start-up timer (ost) ....................................... 281 specifications .......................................................... 385 oscillator switching fail-safe clock monitor ............................................. 26 oscillator, timer1 ..................................................... 105, 115 oscillator, timer3 ............................................................. 113 osctune register ........................................................... 22 p p1a/p1b/p1c/p1d. see enhanced capture/compare/pwm (eccp) .................................................................... 121 packaging information ..................................................... 399 marking .................................................................... 399 pie registers ..................................................................... 75 pie1 register .................................................................... 75 pie2 register .................................................................... 76 pinout descriptions pic18f1xk50/pic18lf1xk50 .................................. 13 pir registers ..................................................................... 73 pir1 register .................................................................... 73 pir2 register .................................................................... 74 pop ................................................................................. 338 por. see power-on reset. porta associated registers ................................................. 88 lata register ........................................................... 83 porta register ........................................................ 83 specifications .......................................................... 383 trisa register .......................................................... 83 porta register ................................................................ 85 portb associated registers ................................................. 93 latb register ........................................................... 89 portb register ........................................................ 89 trisb register .......................................................... 89 portb register .......................................................... 90, 94 portc associated registers ................................................. 97 latc register ........................................................... 94 portc register ........................................................ 94 rc3/sck/scl pin ................................................... 153 specifications .......................................................... 383 trisc register ......................................................... 94 power managed modes ................................................... 235 and a/d operation ................................................... 213 and pwm operation ................................................ 137
pic18f/lf1xk50 ds41350e-page 412 preliminary ? 2010 microchip technology inc. and spi operation ................................................... 147 entering .................................................................... 235 exiting idle and sleep modes .................................. 239 by interrupt ....................................................... 239 by reset ........................................................... 240 by wdt time-out ............................................. 239 without a start-up delay .................................. 240 idle modes ............................................................... 237 pri_idle ......................................................... 238 rc_idle .......................................................... 239 sec_idle ........................................................ 238 multiple sleep functions .......................................... 236 run modes ............................................................... 236 pri_run ......................................................... 236 rc_run .......................................................... 236 sec_run ........................................................ 236 selecting .................................................................. 235 sleep mode .............................................................. 237 summary (table) ...................................................... 235 power-on reset (por) .................................................... 279 power-up timer (pwrt) ......................................... 281 time-out sequence .................................................. 281 power-up timer (pwrt) specifications ........................................................... 385 precision internal oscillator parameters .......................... 383 prescaler, timer0 ............................................................. 103 pri_idle mode ............................................................... 238 pri_run mode ............................................................... 236 program counter ................................................................ 30 pcl, pch and pcu registers ................................... 30 pclath and pclatu registers .............................. 30 program memory and extended instruction set ..................................... 50 code protection ....................................................... 305 instructions ................................................................. 34 two-word .......................................................... 34 interrupt vector .......................................................... 29 look-up tables .......................................................... 32 map and stack (diagram) ........................................... 29 reset vector .............................................................. 29 program verification and code protection ....................... 303 associated registers ............................................... 305 programming, device instructions ................................... 309 pstrcon register ......................................................... 134 pulse steering .................................................................. 134 push ............................................................................... 338 push and pop instructions .............................................. 31 pushl ............................................................................. 354 pwm (eccp module) effects of a reset ..................................................... 137 operation in power managed modes ...................... 137 operation with fail-safe clock monitor ................... 137 pulse steering .......................................................... 134 steering synchronization ......................................... 136 pwm mode. see enhanced capture/compare/pwm ..... 121 pwm1con register ........................................................ 133 r ram. see data memory. rc_idle mode ................................................................ 239 rc_run mode ................................................................ 236 rcall .............................................................................. 339 rcon register .......................................................... 79, 278 bit status during initialization .................................. 284 rcreg ............................................................................ 188 rcsta register ............................................................... 191 reader response ............................................................ 418 recon0 (reference control 0) register ........................ 247 recon1 (reference control 1) register ........................ 248 recon2 (reference control 2) register ........................ 248 register rcreg register ..................................................... 197 register file ....................................................................... 39 register file summary ...................................................... 41 registers adcon0 (adc control 0) ....................................... 215 adcon1 (adc control 1) ............................... 216, 217 adresh (adc result high) with adfm = 0) ......... 218 adresh (adc result high) with adfm = 1) ......... 218 adresl (adc result low) with adfm = 0) ........... 218 adresl (adc result low) with adfm = 1) ........... 218 ansel (analog select 1) .......................................... 98 ansel (port analog control) ................................. 98 anselh (analog select 2) ........................................ 99 anselh (port analog control) .............................. 99 baudcon (eusart baud rate control) .............. 192 bdnstat (buffer descriptor n status, cpu mode) 261 bdnstat (buffer descriptor n status, sie mode) .. 262 ccp1con (enhanced capture/compare/pwm control) .......................................................................... 117 cm1con0 (c1 control) ........................................... 229 cm2con0 (c2 control) ........................................... 230 cm2con1 (c2 control) ........................................... 233 config1h (configuration 1 high) .................. 293, 294 config1l (configuration 1 low) ........................... 293 config2h (configuration 2 high) .......................... 296 config2l (configuration 2 low) ........................... 295 config3h (configuration 3 high) .......................... 297 config4l (configuration 4 low) ........................... 297 config5h (configuration 5 high) .......................... 298 config5l (configuration 5 low) ........................... 298 config6h (configuration 6 high) .......................... 299 config6l (configuration 6 low) ........................... 299 config7h (configuration 7 high) .......................... 300 config7l (configuration 7 low) ........................... 300 devid1 (device id 1) .............................................. 301 devid2 (device id 2) .............................................. 301 eccpas (enhanced ccp auto-shutdown control) 129 eecon1 (data eeprom control 1) ................... 53, 62 intcon (interrupt control) ........................................ 70 intcon2 (interrupt control 2) ................................... 71 intcon3 (interrupt control 3) ................................... 72 ioca (interrupt-on-change porta) ......................... 86 iocb (interrupt-on-change portb) ......................... 91 ipr1 (peripheral interrupt priority 1) ......................... 77 ipr2 (peripheral interrupt priority 2) ......................... 78 lata (porta data latch) ........................................ 86 latb (portb data latch) ........................................ 91 latc (portc data latch) ....................................... 95 osccon (oscillator control) .............................. 20, 21 osctune (oscillator tuning) ................................... 22 pie1 (peripheral interrupt enable 1) .......................... 75 pie2 (peripheral interrupt enable 2) .......................... 76 pir1 (peripheral interrupt request 1) ....................... 73 pir2 (peripheral interrupt request 2) ....................... 74 porta ...................................................................... 85 portb ................................................................ 90, 94 pstrcon (pulse steering control) ........................ 134 pwm1con (enhanced pwm control) .................... 133 rcon (reset control) ....................................... 79, 278 rcsta (receive status and control) ..................... 191
? 2010 microchip technology inc. preliminary ds41350e-page 413 pic18f/lf1xk50 refcon0 ................................................................ 247 refcon1 ................................................................ 248 refcon2 ................................................................ 248 slrcon (port slew rate control) ....................... 100 srcon0 (sr latch control 0) ................................ 242 srcon1 (sr latch control 1) ................................ 243 sspadd (mssp address and baud rate, spi mode) .. 159 sspcon1 (mssp control 1, i 2 c mode) ................. 150 sspcon1 (mssp control 1, spi mode) ................. 141 sspcon2 (mssp control 2, i 2 c mode) ................. 151 sspmsk (ssp mask) .............................................. 158 sspstat (mssp status, spi mode) .............. 140, 149 status ..................................................................... 45 stkptr (stack pointer) ............................................ 31 t0con (timer0 control) .......................................... 101 t1con (timer1 control) .......................................... 105 t2con (timer2 control) .......................................... 111 t3con (timer3 control) .......................................... 113 trisa (tri-state porta) .......................................... 85 trisb (tri-state portb) .................................... 90, 94 txsta (transmit status and control) ..................... 190 ucfg (usb configuration) ...................................... 254 ucon (usb control) ............................................... 252 ueie (usb error interrupt enable) .......................... 270 ueir (usb error interrupt status) ........................... 269 uepn (usb endpoint n control) .............................. 257 uie (usb interrupt enable) ...................................... 268 uir (usb interrupt status) ...................................... 266 ustat (usb status) ............................................... 256 wdtcon (watchdog timer control) ...................... 303 wpua (weak pull-up porta) .................................. 86 wpub (weak pull-up portb) .................................. 91 reset ............................................................................. 339 reset state of registers .................................................. 284 resets ...................................................................... 277, 291 brown-out reset (bor) ........................................... 291 oscillator start-up timer (ost) ............................... 291 power-on reset (por) ............................................ 291 power-up timer (pwrt) ......................................... 291 retfie ............................................................................ 340 retlw ............................................................................ 340 return .......................................................................... 341 return address stack ........................................................ 30 return stack pointer (stkptr) ........................................ 31 revision history ............................................................... 405 rlcf ................................................................................ 341 rlncf ............................................................................. 342 rrcf ............................................................................... 342 rrncf ................. ........................................................... 343 s sck .................................................................................. 139 sdi ................................................................................... 139 sdo ................................................................................. 139 sec_idle mode .............................................................. 238 sec_run mode .............................................................. 236 serial clock, sck ............................................................ 139 serial data in (sdi) .......................................................... 139 serial data out (sdo) ..................................................... 139 serial peripheral interface. see spi mode. setf ................................................................................ 343 shoot-through current ..................................................... 132 single-supply icsp programming. slave select (ss ) ............................................................. 139 slave select synchronization .......................................... 145 sleep ............................................................................. 344 sleep mode ..................................................................... 237 slrcon register ........................................................... 100 software simulator (mplab sim) ................................... 361 spbrg ............................................................................ 193 spbrgh ......................................................................... 193 special event trigger ...................................................... 213 special event trigger. see compare (eccp mode). special features of the cpu ........................................... 291 special function registers ................................................ 39 map ............................................................................ 40 spi mode typical master/slave connection ............................ 143 spi mode (mssp) associated registers ............................................... 147 bus mode compatibility ........................................... 147 effects of a reset .................................................... 147 enabling spi i/o ...................................................... 143 master mode ............................................................ 144 operation ................................................................. 142 operation in power managed modes ...................... 147 serial clock ............................................................. 139 serial data in ........................................................... 139 serial data out ........................................................ 139 slave mode .............................................................. 145 slave select ............................................................. 139 slave select synchronization .................................. 145 spi clock ................................................................. 144 typical connection .................................................. 143 sr latch .......................................................................... 241 associated registers ............................................... 243 srcon0 register ........................................................... 242 srcon1 register ........................................................... 243 ss .................................................................................... 139 ssp typical spi master/slave connection ..................... 143 sspadd register ............................................................ 159 sspcon1 register ................................................. 141, 150 sspcon2 register ......................................................... 151 sspmsk register ........................................................... 158 sspov ............................................................................ 171 sspov status flag ......................................................... 171 sspstat register .................................................. 140, 149 r/w bit ............................................................ 152, 153 stack full/underflow resets .............................................. 32 standard instructions ....................................................... 309 status register .............................................................. 45 stkptr register .............................................................. 31 subfsr .......................................................................... 355 subfwb ......................................................................... 344 sublw ............................................................................ 345 subulnk ........................................................................ 355 subwf ............................................................................ 345 subwfb ......................................................................... 346 swapf ............................................................................ 346 t t0con register .............................................................. 101 t1con register .............................................................. 105 t2con register .............................................................. 111 t3con register .............................................................. 113 table pointer operations (table) ........................................ 54 table reads/table writes ................................................. 32 tblrd ............................................................................. 347 tblwt ............................................................................ 348 thermal considerations ................................................... 377
pic18f/lf1xk50 ds41350e-page 414 preliminary ? 2010 microchip technology inc. time-out in various situations (table) .............................. 281 timer0 .............................................................................. 101 associated registers ............................................... 103 operation ................................................................. 102 overflow interrupt .................................................... 103 prescaler .................................................................. 103 prescaler assignment (psa bit) .............................. 103 prescaler select (t0ps2:t0ps0 bits) ..................... 103 prescaler. see prescaler, timer0. reads and writes in 16-bit mode ............................ 102 source edge select (t0se bit) ................................ 102 source select (t0cs bit) ......................................... 102 specifications ........................................................... 386 switching prescaler assignment .............................. 103 timer1 .............................................................................. 105 16-bit read/write mode ........................................... 107 associated registers ............................................... 110 interrupt .................................................................... 108 operation ................................................................. 106 oscillator .......................................................... 105, 107 oscillator layout considerations ............................. 108 overflow interrupt .................................................... 105 resetting, using the ccp special event trigger ..... 108 specifications ........................................................... 386 tmr1h register ...................................................... 105 tmr1l register ....................................................... 105 use as a real-time clock ....................................... 109 timer2 .............................................................................. 111 associated registers ............................................... 112 interrupt .................................................................... 112 operation ................................................................. 111 output ...................................................................... 112 timer3 .............................................................................. 113 16-bit read/write mode ........................................... 115 associated registers ............................................... 116 operation ................................................................. 114 oscillator .......................................................... 113, 115 overflow interrupt ............................................ 113, 115 special event trigger (ccp) .................................... 116 tmr3h register ...................................................... 113 tmr3l register ....................................................... 113 timing diagrams a/d conversion ........................................................ 388 acknowledge sequence .......................................... 174 asynchronous reception ......................................... 188 asynchronous transmission .................................... 185 asynchronous transmission (back to back) ........... 185 auto wake-up bit (wue) during normal operation 199 auto wake-up bit (wue) during sleep ................... 199 automatic baud rate calculator .............................. 197 baud rate generator with clock arbitration ............ 168 brg reset due to sda arbitration during start condi- tion ................................................................... 177 brown-out reset (bor) ........................................... 384 bus collision during a repeated start condition (case 1) ...................................................................... 178 bus collision during a repeated start condition (case 2) ...................................................................... 178 bus collision during a start condition (scl = 0) .... 177 bus collision during a stop condition (case 1) ...... 179 bus collision during a stop condition (case 2) ...... 179 bus collision during start condition (sda only) ..... 176 bus collision for transmit and acknowledge ........... 175 clkout and i/o ...................................................... 383 clock synchronization ............................................. 161 clock timing ............................................................ 379 clock/instruction cycle .............................................. 33 comparator output .................................................. 223 enhanced capture/compare/pwm (eccp) ............ 387 fail-safe clock monitor (fscm) ................................ 27 first start bit timing ................................................ 169 full-bridge pwm output .......................................... 126 half-bridge pwm output ................................. 124, 132 i 2 c bus data ............................................................ 394 i 2 c bus start/stop bits ............................................ 393 i 2 c master mode (7 or 10-bit transmission) ........... 172 i 2 c master mode (7-bit reception) .......................... 173 i 2 c slave mode (10-bit recepti on, sen = 0) .......... 156 i 2 c slave mode (10-bit recepti on, sen = 1) .......... 163 i 2 c slave mode (10-bit transmission) .................... 157 i 2 c slave mode (7-bit reception, sen = 0) ............ 154 i 2 c slave mode (7-bit reception, sen = 1) ............ 162 i 2 c slave mode (7-bit transmission) ...................... 155 i 2 c slave mode general call address sequence (7 or 10-bit address mode) ...................................... 164 i 2 c stop condition receive or transmit mode ........ 174 internal oscillator switch timing ............................... 23 pwm auto-shutdown auto-restart enabled ........................................ 131 firmware restart ............................................. 130 pwm direction change ........................................... 127 pwm direction change at near 100% duty cycle .. 128 pwm output (active-high) ...................................... 122 pwm output (active-low) ....................................... 123 repeat start condition ............................................ 170 reset, wdt, ost and power-up timer .................. 384 send break character sequence ............................ 200 slave synchronization ............................................. 145 slow rise time (mclr tied to v dd , v dd rise > t pwrt ) .......................................................................... 283 spi master mode (cke = 1, smp = 1) .................... 391 spi mode (master mode) ......................................... 144 spi mode (slave mode, cke = 0) ........................... 146 spi mode (slave mode, cke = 1) ........................... 146 spi slave mode (cke = 0) ...................................... 392 spi slave mode (cke = 1) ...................................... 392 synchronous reception (master mode, sren) ...... 204 synchronous transmission ..................................... 202 synchronous transmission (through txen) .......... 202 time-out sequence on por w/pll enabled (mclr tied to v dd ) ............................................................. 283 time-out sequence on power-up (mclr not tied to v dd , case 1) ................................................... 282 time-out sequence on power-up (mclr not tied to v dd , case 2) ................................................... 282 time-out sequence on power-up (mclr tied to v dd , v dd rise < t pwrt ) .......................................... 282 timer0 and timer1 external clock .......................... 386 transition for entry to sleep mode .......................... 237 transition for wake from sleep (hspll) ................ 237 transition timing for entry to idle mode .................. 238 transition timing for wake from idle to run mode . 238 usart synchronous receive (master/slave) ........ 390 usart synchronous transmission (master/slave) 390 timing diagrams and specifications a/d conversion requirements ................................ 388 pll clock ................................................................ 382 timing parameter symbology ......................................... 378 timing requirements i 2 c bus data ............................................................ 395
? 2010 microchip technology inc. preliminary ds41350e-page 415 pic18f/lf1xk50 i2c bus start/stop bits ............................................ 394 spi mode ................................................................. 393 top-of-stack access .......................................................... 30 trisa register .................................................................. 85 trisb register ............................................................ 90, 94 tstfsz ........................................................................... 349 two-speed start-up ......................................................... 291 two-word instructions example cases .......................................................... 34 txreg ............................................................................. 183 txsta register ............................................................... 190 brgh bit ................................................................. 193 u universal serial bus address register (uaddr) ..................................... 258 associated registers ............................................... 274 buffer descriptor table ............................................ 259 buffer descriptors .................................................... 259 address validation ........................................... 262 assignment in different buffering modes ........ 264 bdnstat register (cpu mode) ..................... 260 bdnstat register (sie mode) ....................... 262 byte count ....................................................... 262 example ........................................................... 259 memory map .................................................... 263 ownership ........................................................ 259 ping-pong buffering ......................................... 263 register summary ........................................... 264 status and configuration ................................. 259 class specifications and drivers ............................. 276 descriptors ............................................................... 276 endpoint control ...................................................... 257 enumeration ............................................................. 276 external pull-up resistors ........................................ 255 eye pattern test enable .......................................... 255 firmware and drivers ............................................... 274 frame number registers ......................................... 258 frames ..................................................................... 275 internal pull-up resistors ......................................... 255 internal transceiver ................................................. 253 interrupts .................................................................. 265 and usb transactions ..................................... 265 layered framework ................................................. 275 oscillator requirements ........................................... 274 overview .......................................................... 251, 275 ping-pong buffer configuration ............................... 255 power ....................................................................... 275 power modes ........................................................... 271 bus power only ............................................... 271 dual power with self-power dominance ......... 272 self-power only ............................................... 271 ram ......................................................................... 258 memory map .................................................... 258 speed ....................................................................... 276 status and control ................................................... 252 transfer types ......................................................... 275 ufrmh:ufrml registers ...................................... 258 usart synchronous master mode requirements, synchronous receive ............. 390 requirements, synchronous transmission ..... 390 timing diagram, synchronous receive .......... 390 timing diagram, synchronous transmission .. 390 usb module electrical specifications .............................. 376 usb ram serial interface engine (sie) ..................................... 35 usb. see universal serial bus. v voltage reference (vr) specifications .......................................................... 389 voltage reference. see comparator voltage reference (cv ref ) voltage references fixed voltage reference (fvr) .............................. 246 vr stabilization ....................................................... 246 v ref . s ee adc reference voltage w wake-up on break ........................................................... 198 watchdog timer (wdt) ........................................... 291, 302 associated registers ............................................... 303 control register ....................................................... 303 programming considerations .................................. 302 specifications .......................................................... 385 wcol ...................................................... 169, 170, 171, 174 wcol status flag ................................... 169, 170, 171, 174 wdtcon register .......................................................... 303 wpua register .................................................................. 86 wpub register .................................................................. 91 www address ................................................................ 417 www, on-line support .................... .................................. 7 x xorlw ........................................................................... 349 xorwf ........................................................................... 350
pic18f/lf1xk50 ds41350e-page 416 preliminary ? 2010 microchip technology inc. notes:
? 2010 microchip technology inc. preliminary ds41350e-page 417 pic18f1xk50/pic18lf1xk50 the microchip web site microchip provides online support via our www site at www.microchip.com . this web site is used as a means to make files and information easily available to customers. accessible by using your favorite internet browser, the web site contains the following information: ? product support ? data sheets and errata, application notes and sample programs, design resources, user?s guides and hardware support documents, latest software releases and archived software ? general technical support ? frequently asked questions (faq), technical support requests, online discussion groups, microchip consultant program member listing ? business of microchip ? product selector and ordering guides, latest microchip press releases, listing of seminars and events, listings of microchip sales offices, distributors and factory representatives customer change notification service microchip?s customer notification service helps keep customers current on microchip products. subscribers will receive e-mail notification whenever there are changes, updates, revisions or errata related to a specified product family or development tool of interest. to register, access the microchip web site at www.microchip.com . under ?support?, click on ?customer change notification? and follow the registration instructions. customer support users of microchip products can receive assistance through several channels: ? distributor or representative ? local sales office ? field application engineer (fae) ? technical support ? development systems information line customers should contact their distributor, representative or field application engineer (fae) for support. local sales offices are also available to help customers. a listing of sales offices and locations is included in the back of this document. technical support is available through the web site at: http://support.microchip.com
pic18f1xk50/pic18lf1xk50 ds41350e-page 418 preliminary ? 2010 microchip technology inc. reader response it is our intention to provide you with the best documentation possible to ensure successful use of your microchip product. if you wish to provide your comments on organization, clarity, subject matter, and ways in which our documentation can better serve you, please fax your comments to the technical publications manager at (480) 792-4150. please list the following information, and use this outline to provide us with your comments about this document. to: technical publications manager re: reader response total pages sent ________ from: name company address city / state / zip / country telephone: (_______) _________ - _________ application (optional): would you like a reply? y n device: literature number: questions: fax: (______) _________ - _________ ds41350e pic18f1xk50/pic18lf1xk50 1. what are the best features of this document? 2. how does this document meet your hardware and software development needs? 3. do you find the organization of this document easy to follow? if not, why? 4. what additions to the document do you think would enhance the structure and subject? 5. what deletions from the document could be made without affecting the overall usefulness? 6. is there any incorrect or misleading information (what and where)? 7. how would you improve this document?
? 2010 microchip technology inc. preliminary ds41350e-page 419 pic18f/lf1xk50 product identification system to order or obtain information, e. g., on pricing or delivery, refer to the factory or the listed sales office . part no. x /xx xxx pattern package temperature range device device: pic18f13k50 (1) , pic18f14k50 (1) , pic18lf13k50 (1) , pic18lf14k50 packaging option: blank = standard packaging (tube or tray) t = tape and reel (1) temperature range: e= -40 ? c to +125 ? c (extended) i = -40c to +85c (industrial) package: p=pdip so = soic ss = ssop mq = qfn pattern: qtp, sqtp, code or special requirements (blank otherwise) examples: a) pic18f14k50-e/p 301 = extended temp., pdip package, extended v dd limits, qtp pat- tern #301. b) pic18lf14k50-e/so = extended temp., soic package. c) pic18lf14k50-e/p = extended temp., pdip package. d) pic18lf14k50-e/mq = extended temp., qfn package. e) pic18f14k50-i/p = industrial temp., pdip package. note 1: tape and reel option is available for ml, mv, pt, so and ss packages with industrial temperature range only. x packaging option
ds41350e-page 420 preliminary ? 2010 microchip technology inc. americas corporate office 2355 west chandler blvd. chandler, az 85224-6199 tel: 480-792-7200 fax: 480-792-7277 technical support: http://support.microchip.com web address: www.microchip.com atlanta duluth, ga tel: 678-957-9614 fax: 678-957-1455 boston westborough, ma tel: 774-760-0087 fax: 774-760-0088 chicago itasca, il tel: 630-285-0071 fax: 630-285-0075 cleveland independence, oh tel: 216-447-0464 fax: 216-447-0643 dallas addison, tx tel: 972-818-7423 fax: 972-818-2924 detroit farmington hills, mi tel: 248-538-2250 fax: 248-538-2260 kokomo kokomo, in tel: 765-864-8360 fax: 765-864-8387 los angeles mission viejo, ca tel: 949-462-9523 fax: 949-462-9608 santa clara santa clara, ca tel: 408-961-6444 fax: 408-961-6445 toronto mississauga, ontario, canada tel: 905-673-0699 fax: 905-673-6509 asia/pacific asia pacific office suites 3707-14, 37th floor tower 6, the gateway harbour city, kowloon hong kong tel: 852-2401-1200 fax: 852-2401-3431 australia - sydney tel: 61-2-9868-6733 fax: 61-2-9868-6755 china - beijing tel: 86-10-8528-2100 fax: 86-10-8528-2104 china - chengdu tel: 86-28-8665-5511 fax: 86-28-8665-7889 china - chongqing tel: 86-23-8980-9588 fax: 86-23-8980-9500 china - hong kong sar tel: 852-2401-1200 fax: 852-2401-3431 china - nanjing tel: 86-25-8473-2460 fax: 86-25-8473-2470 china - qingdao tel: 86-532-8502-7355 fax: 86-532-8502-7205 china - shanghai tel: 86-21-5407-5533 fax: 86-21-5407-5066 china - shenyang tel: 86-24-2334-2829 fax: 86-24-2334-2393 china - shenzhen tel: 86-755-8203-2660 fax: 86-755-8203-1760 china - wuhan tel: 86-27-5980-5300 fax: 86-27-5980-5118 china - xian tel: 86-29-8833-7252 fax: 86-29-8833-7256 china - xiamen tel: 86-592-2388138 fax: 86-592-2388130 china - zhuhai tel: 86-756-3210040 fax: 86-756-3210049 asia/pacific india - bangalore tel: 91-80-3090-4444 fax: 91-80-3090-4123 india - new delhi tel: 91-11-4160-8631 fax: 91-11-4160-8632 india - pune tel: 91-20-2566-1512 fax: 91-20-2566-1513 japan - yokohama tel: 81-45-471- 6166 fax: 81-45-471-6122 korea - daegu tel: 82-53-744-4301 fax: 82-53-744-4302 korea - seoul tel: 82-2-554-7200 fax: 82-2-558-5932 or 82-2-558-5934 malaysia - kuala lumpur tel: 60-3-6201-9857 fax: 60-3-6201-9859 malaysia - penang tel: 60-4-227-8870 fax: 60-4-227-4068 philippines - manila tel: 63-2-634-9065 fax: 63-2-634-9069 singapore tel: 65-6334-8870 fax: 65-6334-8850 taiwan - hsin chu tel: 886-3-6578-300 fax: 886-3-6578-370 taiwan - kaohsiung tel: 886-7-213-7830 fax: 886-7-330-9305 taiwan - taipei tel: 886-2-2500-6610 fax: 886-2-2508-0102 thailand - bangkok tel: 66-2-694-1351 fax: 66-2-694-1350 europe austria - wels tel: 43-7242-2244-39 fax: 43-7242-2244-393 denmark - copenhagen tel: 45-4450-2828 fax: 45-4485-2829 france - paris tel: 33-1-69-53-63-20 fax: 33-1-69-30-90-79 germany - munich tel: 49-89-627-144-0 fax: 49-89-627-144-44 italy - milan tel: 39-0331-742611 fax: 39-0331-466781 netherlands - drunen tel: 31-416-690399 fax: 31-416-690340 spain - madrid tel: 34-91-708-08-90 fax: 34-91-708-08-91 uk - wokingham tel: 44-118-921-5869 fax: 44-118-921-5820 worldwide sales and service 08/04/10


▲Up To Search▲   

 
Price & Availability of PIC18F-LF1XK50

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X